Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对文件进行脚本搜索,然后读取找到的文件。python 3.5_Python_Python 3.x - Fatal编程技术网

对文件进行脚本搜索,然后读取找到的文件。python 3.5

对文件进行脚本搜索,然后读取找到的文件。python 3.5,python,python-3.x,Python,Python 3.x,idk这里怎么了,有点不管用。 因此,我有一个名为setup.py的安装程序脚本,它安装目录。但是,它还会生成一个名为setup.txt的文件,告诉主脚本不要再次尝试安装它。但是不管我是否有setup.txt文件,它都没有任何作用。 任何帮助都将不胜感激 import time import os import setup import glob def install_checker(): setup_file = open("setup.txt","

idk这里怎么了,有点不管用。 因此,我有一个名为setup.py的安装程序脚本,它安装目录。但是,它还会生成一个名为setup.txt的文件,告诉主脚本不要再次尝试安装它。但是不管我是否有setup.txt文件,它都没有任何作用。 任何帮助都将不胜感激

import time
import os
import setup
import glob


def install_checker():
    setup_file = open("setup.txt","r")
    install_checker = setup_file.readline(24)

    if install_checker == "setup completion = true":
          pass
    else:
        setup.setup()

mydir = os.getcwd()
os.chdir(mydir)
for file in glob.glob("setup.txt"):
    print(file)

    if file == "setup.txt":
        install_checker()
    else:
        setup.setup()```

有很多方法可以检查文件是否存在。这里有一个:

if os.path.isfile("setup.txt"):
    setup.setup()
else:
    install_checker()

我不建议尝试打开该文件并捕获错误,因为该文件可能存在但不可读。

请尝试读取该文件。如果抛出错误,则该文件不存在。如果没有错误,它会执行。还可以使用
和open(…)作为setup_file
,这样当您阅读完文件时,它会关闭该文件。输入一些
print
语句以了解正在执行的代码行。我认为它不起作用,因为使用setup_file.readline(24)时,返回如下:“setup completion=true\n”。也许这就是什么都不做的原因?谢谢,我应该记住这一点。