我有两个python脚本
script1.py1: -
def somefunction()
.................
out_file = open("out.txt","w")
#creates a file out.txt and write something into it
out_file.close()
.................
.................
somefunction() #calling somefunction
out_file = open("out.txt","r")
output = open("Final_out.txt","w")
for line in out_file:
match_for_pattern = re.search(r"^\(.*affected\)$",line)
if match_for_pattern:
output.write("")
else:
output.write("%s" % line)
out_file.close()
output.close()
我想在out.txt文件中更改某些内容,即删除一些不需要的行,所以我想创建一个新文件Final_out.txt,其中包含所有更改。
但我在运行代码
时收到权限被拒绝错误错误: -
output = open("Final_out.txt","w")
IOError: [Errno 13] Permission denied: 'Final_out.txt'
我目前的工作目录中有完整的权限chmod 777
如果我取出我在我的文件中更改内容的代码(out.txt)并将其放入一个新的python脚本(script2.py),那么它正在工作。
scritp2.py: -
out_file = open("out.txt","r")
output = open("Final_out.txt","w")
for line in out_file:
match_for_pattern = re.search(r"^\(.*affected\)$",line)
if match_for_pattern:
output.write("")
else:
output.write("%s" % line)
out_file.close()
output.close()
script2.py在我目前的目录中创建一个文件Final_out.txt,其中包含我想要的所有更改。
我的script1.py无法做到这一点,但script2.py是否可以做到这一点而没有任何错误?