Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.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脚本中写入_Python - Fatal编程技术网

文件未打开,无法在Python脚本中写入

文件未打开,无法在Python脚本中写入,python,Python,我的脚本不工作,我无法找出错误在哪里,每次我想在Python中使用文件时,我都使用open()函数打开了该文件,运行它时会显示此错误: Traceback (most recent call last): File "my_example.py", line 26, in <module> doc.truncate() IOError: File not open for writing 以下是Python脚本(代码): 有解决办法吗 程序只需读取(read())一个文

我的脚本不工作,我无法找出错误在哪里,每次我想在Python中使用文件时,我都使用open()函数打开了该文件,运行它时会显示此错误:

Traceback (most recent call last):
  File "my_example.py", line 26, in <module>
    doc.truncate()
IOError: File not open for writing
以下是Python脚本(代码):

有解决办法吗


程序只需读取(read())一个文件(打印文件),询问用户是否要删除文件(truncate()),以及是否要写入文件(write())。

默认情况下,打开文件进行读取。如果要打开以进行写入,则必须将第二个open参数传递给特定的模式(例如,用于写入的
'w'

默认情况下,打开以进行读取。如果要打开进行写入,还必须将open的第二个参数传递给特定的模式(例如,用于写入的
'w'

成功了,现在脚本运行得很好,还有一件事,我真的需要在何时关闭()打开()文件?例如,我写注释#doc.close()的地方是为了在用print doc.read()打印文件后关闭文件,我是否应该在处理完所有打开的文件后立即关闭它们?或者我可以在程序结束时关闭这些文件,如果我打算再次使用它们(取决于if语句),正如我的程序所示;或者,您可以使用
with
构造在with块结束时结束它们,以避免它们在较长的脚本中打开较长的时间。在以其他模式重新打开文件之前,您肯定要先关闭文件。这样做很有效,现在脚本运行得很好,还有一件事,我真的需要在什么时候关闭()打开的()文件?例如,我写注释#doc.close()的地方是为了在用print doc.read()打印文件后关闭文件,我是否应该在处理完所有打开的文件后立即关闭它们?或者我可以在程序结束时关闭这些文件,如果我打算再次使用它们(取决于if语句),正如我的程序所示;或者,您可以使用
with
构造在with块结束时结束它们,以避免它们在较长的脚本中打开较长的时间。在以其他模式重新打开文件之前,您肯定要先关闭该文件。
python my_example.py my_example_sample.txt
from sys import argv
#from os.path import exists

script, filename = argv

print "The name of this program is %s" % script
print "The name of the file you want to read is %s" % filename
print "Press ENTER if you want to read the selected document."
print "Press CTRL-C to cancel."

raw_input('>')

print "%s :" % filename

doc = open(filename)

print doc.read()

#doc.close()

erase_file = raw_input("Do you want to erase the file %s Y/N? : " % filename)

if erase_file == "Y":
    doc = open(filename)
    print "Truncating the file..."
    doc.truncate()
    print "Done, truncated."
    #doc.close()
else:
    print "That's okay!"

write_file = raw_input("Do you want to write in the file %s Y/N? : " % filename) 
if write_file == "Y":
    doc = open(filename)
    print "I'm going to ask you to type in what you like to write in the file %s 
    (limited to 3 lines)" % filename
    line1 = raw_input("line 1: ")
    line2 = raw_input("line 2: ")
    line3 = raw_input("line 3: ")
    print "Perfect! writing in..."
    doc.write(line1)
    doc.write('\n')
    doc.write(line2)
    doc.write('\n')
    doc.write(line3)
    doc.write('\n')
    print "Done!"
    doc.close()
else:
    print "Ok, see you later!"
    doc.close()
# add copy and exists? features?