Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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 2.7 Can';t在python中生成正确的文件_Python 2.7_File - Fatal编程技术网

Python 2.7 Can';t在python中生成正确的文件

Python 2.7 Can';t在python中生成正确的文件,python-2.7,file,Python 2.7,File,我试图在现有文件的基础上生成一个新文件,该文件只包含一些预定义文本的行。我有: with open("steps_shown_at_least_once.log", "r") as f: for line in f: if line.find("Run program"): output = open('run_studio.txt', 'a') output.write(line) output.c

我试图在现有文件的基础上生成一个新文件,该文件只包含一些预定义文本的行。我有:

with open("steps_shown_at_least_once.log", "r") as f:
    for line in f:
        if line.find("Run program"):
            output = open('run_studio.txt', 'a')
            output.write(line)
            output.close()
出于某种原因,这会生成一个相同的文件。但是,我正在搜索的运行程序不在旧文件的每一行中。

行。find('Run program')返回字符串的索引

返回值
如果找到索引,则为-1,否则为-1

可在此处找到:

而不是
line.find(“Run program”):
write
if“Run program”in line: