如何读取刚刚由python中的函数创建的文件
Posted
技术标签:
【中文标题】如何读取刚刚由python中的函数创建的文件【英文标题】:How to read a file that was just created by a function in python 【发布时间】:2019-12-24 08:47:16 【问题描述】:当我尝试读取刚刚由上面的函数创建的文件时,我得到了空白行。当我在不同的文件中运行相同的代码时,它只运行文件。我看到那个文件已经关闭了。但是当我尝试打开并打印时。我得到空行。
我已将尝试读取的相同代码复制到不同的测试文件并运行该代码。我正在阅读文件并打印行并关闭文件。
def running_symm_list_cmd():
try:
path_to_output_file = "symm_list_out.txt"
myoutput = open(path_to_output_file, 'w')
symm_list = "sudo symcfg list"
Popen([symm_list], shell=True, stdout=myoutput, stderr=myoutput, universal_newlines=True)
except OSError or ValueError as error_str:
print(error_str)
os.system(exit(2))
finally:
myoutput.close()
print("checking if file is closed or not ".format(myoutput.closed))
def getting_symm_list():
try:
print("INSIDE getting_symm_list TRY function")
symm_list =
path = "/users/pjinde/scripts/symm_list_out.txt"
symm_list_file = open(path, 'r')
print("file mode is m and file name is n".format(m=symm_list_file.mode, n=symm_list_file.name))
for line in symm_list_file.readlines():
#print(line)
symm_ids = re.search(r'(000197\d+)\s+\w+.*', line, re.M | re.I)
if symm_ids:
symm_list[symm_ids.group(1)] = None
#print("Symmetrix id is ".format(symm_ids.group(1)))
for ids in symm_list.keys():
print("symmetrix_id are ".format(ids))
except OSError as err:
print("unable to get symm list due to error ".format(err))
finally:
symm_list_file.close()
return symm_list
我期待低于输出。这说明我能够创建字典 symm_list。
INSIDE getting_symm_list TRY function
file mode is r and file name is /users/abcd/efcg/symm_list_out.txt
symmetrix_id are 00019780aaaa
symmetrix_id are 00019780aabc
symmetrix_id are 00019780aadd
symmetrix_id are 00019780aaee
symmetrix_id are 00019780aaff
symmetrix_id are 00019780aaeg
symmetrix_id are 00019790bbbb
非常感谢任何帮助!谢谢。
【问题讨论】:
由于 Popen 运行不对称,您是否尝试过等待 Popen 完成后再关闭文件? p = Popen(...) p.wait() 只是一个快速的刺,没有 mutchoughtt 我认为你是对的@Svavelsyra。我使用了 subprocess.call 而不是 Popen。我打算使用 Popen,因为我打算通过管道来执行 shell 命令。我改变主意了。 【参考方案1】:Popen 异步运行,或者我使用 Popen.wait() 直到子进程完成。或者我们可以只使用 subprocess.call
def running_symm_list_cmd():
try:
path_to_output_file = "symm_list_out.txt"
myoutput = open(path_to_output_file, 'w')
symm_list = "sudo symcfg list"
p1 = subprocess.call([symm_list], shell=True, stdout=myoutput, stderr=myoutput)
except OSError or ValueError as error_str:
print(error_str)
os.system(exit(2))
finally:
myoutput.close()
print("checking if file is closed or not ".format(myoutput.closed))
def getting_symm_list():
try:
symm_list =
path = "/users/pjinde/scripts/symm_list_out.txt"
symm_list_file = open(path, 'r')
#print("file mode is m and file name is n".format(m=symm_list_file.mode, n=symm_list_file.name))
for line in symm_list_file.readlines():
#print(line)
symm_ids = re.search(r'(000197\d+)\s+\w+.*', line, re.M | re.I)
if symm_ids:
symm_list[symm_ids.group(1)] = None
#print("Symmetrix id is ".format(symm_ids.group(1)))
for ids in symm_list.keys():
print("symmetrix_id are ".format(ids))
except OSError as err:
print("unable to get symm list due to error ".format(err))
finally:
symm_list_file.close()
return symm_list
【讨论】:
以上是关于如何读取刚刚由python中的函数创建的文件的主要内容,如果未能解决你的问题,请参考以下文章