Python3中的文件函数
您好,我是python的新手(我已经学习了3天了),对于您的帮助,我将不胜感激 所以我做了下面的函数,创建了名为oldfile,newfile的2.txt文档。我在旧文件中写入了64个字符(如“asasdasffafdsfgsdgs”)。函数应该打开这两个文件并将新文件中的50个字符写入新文件,但当我在pyscripter中按start时,代码可以工作,但它不做任何事情(它不会将任何内容写入newfile.txt)Python3中的文件函数,python,python-3.x,Python,Python 3.x,您好,我是python的新手(我已经学习了3天了),对于您的帮助,我将不胜感激 所以我做了下面的函数,创建了名为oldfile,newfile的2.txt文档。我在旧文件中写入了64个字符(如“asasdasffafdsfgsdgs”)。函数应该打开这两个文件并将新文件中的50个字符写入新文件,但当我在pyscripter中按start时,代码可以工作,但它不做任何事情(它不会将任何内容写入newfile.txt) 您需要调用您的函数: def copy_file(oldfile, newfil
您需要调用您的函数:
def copy_file(oldfile, newfile):
infile = open(oldfile, 'r')
outfile = open(newfile, 'w')
while True:
text = infile.read(50)
if text == "":
break
outfile.write(text)
infile.close()
outfile.close()
return
copy_file('old_file_name_path', 'new_file_name_path') # call your function with path of file here
你调用过你的函数吗???你的代码是正确的,它正在工作,只需调用你使用windows或linux的函数??谢谢你的回复,但它仍然给我一个错误。我的路径有问题。如果两个文件都在桌面上,请在那里写下确切的路径好吗?
def copy_file(oldfile, newfile):
infile = open(oldfile, 'r')
outfile = open(newfile, 'w')
while True:
text = infile.read(50)
if text == "":
break
outfile.write(text)
infile.close()
outfile.close()
return
copy_file('old_file_name_path', 'new_file_name_path') # call your function with path of file here