通过将文件作为参数传递给python脚本中的函数来写入和读取新文件
我有一个python脚本,它从命令提示符获取输入文件名。我创建了一个列表来存储所有输入文件,并将其传递给一个函数,以创建一个新文件,同时合并所有输入文件。现在,我将这个新编写的文件作为输入传递给另一个函数。我收到一条错误消息 TypeError:强制使用Unicode:需要字符串或缓冲区,找到列表 代码:通过将文件作为参数传递给python脚本中的函数来写入和读取新文件,python,file,function,cmd,os.path,Python,File,Function,Cmd,Os.path,我有一个python脚本,它从命令提示符获取输入文件名。我创建了一个列表来存储所有输入文件,并将其传递给一个函数,以创建一个新文件,同时合并所有输入文件。现在,我将这个新编写的文件作为输入传递给另一个函数。我收到一条错误消息 TypeError:强制使用Unicode:需要字符串或缓冲区,找到列表 代码: 我在带有单词的行中突出显示了此错误。open()。我试图打印result.log文件的路径。它在0x000000000227578打印了关闭的文件“result.log”,模式为“w”。我哪里
我在带有单词的行中突出显示了此错误。open()。我试图打印result.log文件的路径。它在0x000000000227578打印了关闭的文件“result.log”,模式为“w”。我哪里做错了?如何创建新文件并将其传递给函数 您的问题是
结果
是一个关闭的文件对象:
start_process(result,process_name)
我想你想要
start_process('result.log', process_name)
你可以用它来清理一下脚本
import shutil
file_list = sys.argv[1:-1]
process_name = sys.argv[-1]
integrate_files(file_list,process_name)
def integrate_files(file_list,process_name):
with open('result.log', 'w' ) as result:
for file_ in file_list:
with open(file_) as infile:
shutil.copyfileobj(infile, result)
start_process('result.log',process_name)
def start_process(result,process_name):
with open(result,'r') as mainFile:
content = mainFile.readlines()
您的问题是
result
是一个关闭的文件对象:
start_process(result,process_name)
我想你想要
start_process('result.log', process_name)
你可以用它来清理一下脚本
import shutil
file_list = sys.argv[1:-1]
process_name = sys.argv[-1]
integrate_files(file_list,process_name)
def integrate_files(file_list,process_name):
with open('result.log', 'w' ) as result:
for file_ in file_list:
with open(file_) as infile:
shutil.copyfileobj(infile, result)
start_process('result.log',process_name)
def start_process(result,process_name):
with open(result,'r') as mainFile:
content = mainFile.readlines()
您的问题是
result
是一个关闭的文件对象:
start_process(result,process_name)
我想你想要
start_process('result.log', process_name)
你可以用它来清理一下脚本
import shutil
file_list = sys.argv[1:-1]
process_name = sys.argv[-1]
integrate_files(file_list,process_name)
def integrate_files(file_list,process_name):
with open('result.log', 'w' ) as result:
for file_ in file_list:
with open(file_) as infile:
shutil.copyfileobj(infile, result)
start_process('result.log',process_name)
def start_process(result,process_name):
with open(result,'r') as mainFile:
content = mainFile.readlines()
您的问题是
result
是一个关闭的文件对象:
start_process(result,process_name)
我想你想要
start_process('result.log', process_name)
你可以用它来清理一下脚本
import shutil
file_list = sys.argv[1:-1]
process_name = sys.argv[-1]
integrate_files(file_list,process_name)
def integrate_files(file_list,process_name):
with open('result.log', 'w' ) as result:
for file_ in file_list:
with open(file_) as infile:
shutil.copyfileobj(infile, result)
start_process('result.log',process_name)
def start_process(result,process_name):
with open(result,'r') as mainFile:
content = mainFile.readlines()
问题在于:
with open('result.log', 'w' ) as result:
# ...
start_process(result,process_name)
由于您在start\u process
中重新打开了文件,您只需传递名称:
start_process(result.name, process_name)
或者直接说:
start_process('result.log', process_name)
问题在于:
with open('result.log', 'w' ) as result:
# ...
start_process(result,process_name)
由于您在start\u process
中重新打开了文件,您只需传递名称:
start_process(result.name, process_name)
或者直接说:
start_process('result.log', process_name)
问题在于:
with open('result.log', 'w' ) as result:
# ...
start_process(result,process_name)
由于您在start\u process
中重新打开了文件,您只需传递名称:
start_process(result.name, process_name)
或者直接说:
start_process('result.log', process_name)
问题在于:
with open('result.log', 'w' ) as result:
# ...
start_process(result,process_name)
由于您在start\u process
中重新打开了文件,您只需传递名称:
start_process(result.name, process_name)
或者直接说:
start_process('result.log', process_name)
当您以open('result.log','w')作为结果写入
时:
,将result
设置为表示磁盘上实际文件的对象。这与文件名不同
您当然可以将该结果
传递给另一个函数。但是,由于它将是实际的文件对象,而不是文件名,因此不能将其传递给open
-open
需要文件名,并查找具有该名称的文件,以便创建新的文件对象
您可以调用该文件对象上的方法,但它们都不会真正重新打开该文件。相反,最简单的事情是记住并传递文件名,这样start\u进程
就可以再次打开它
如@matsjoyce的回答所示,file对象记住了原始文件名。因此,您可以传递对象,并让start\u进程
获取名称。但那太乱了。真的,把名字传过去就行了。(正如mats所示,您可以显式地传递result.name
,而不是先创建自己的name
变量)。传递文件对象通常不是您想要的-仅当您希望在函数之间分割读/写工作时才这样做(并且有很好的理由)。当您使用open('result.log','w')编写时,作为结果:
,您将结果
作为表示磁盘上实际文件的对象。这与文件名不同
您当然可以将该结果
传递给另一个函数。但是,由于它将是实际的文件对象,而不是文件名,因此不能将其传递给open
-open
需要文件名,并查找具有该名称的文件,以便创建新的文件对象
您可以调用该文件对象上的方法,但它们都不会真正重新打开该文件。相反,最简单的事情是记住并传递文件名,这样start\u进程
就可以再次打开它
如@matsjoyce的回答所示,file对象记住了原始文件名。因此,您可以传递对象,并让start\u进程
获取名称。但那太乱了。真的,把名字传过去就行了。(正如mats所示,您可以显式地传递result.name
,而不是先创建自己的name
变量)。传递文件对象通常不是您想要的-仅当您希望在函数之间分割读/写工作时才这样做(并且有很好的理由)。当您使用open('result.log','w')编写时,作为结果:
,您将结果
作为表示磁盘上实际文件的对象。这与文件名不同
您当然可以将该结果
传递给另一个函数。但是,由于它将是实际的文件对象,而不是文件名,因此不能将其传递给open
-open
需要文件名,并查找具有该名称的文件,以便创建新的文件对象
您可以调用该文件对象上的方法,但它们都不会真正重新打开该文件。相反,最简单的事情是记住并传递文件名,这样start\u进程
就可以再次打开它
如@matsjoyce的回答所示,file对象记住了原始文件名。因此,您可以传递对象,并让start\u进程
获取名称。但那太乱了。真的,把名字传过去就行了。(正如mats所示,您可以显式地传递result.name
,而不是先创建自己的name
变量)。传递文件对象通常不是您想要的-仅当您希望在函数之间分割读/写工作时才这样做(并且有很好的理由)。当您使用open('result.log','w')编写时,作为结果:
,您将结果
作为表示磁盘上实际文件的对象。这与文件名不同