python3中的文件函数

2024-09-24 12:29:41 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我是python的新成员(我已经学了3天了),感谢您的帮助。在

所以我做了这个函数,你看下面,创建了2.txt文件,叫做oldfile,newfile。在旧文件中我写了64个字符(比如“asasdasffafdsfgsgsdgs”)。这个函数应该打开两个文件并从新文件中写入50个字符,但是当我在pyscripter中按start时,代码可以工作,但是它什么也不做(它不会向新文件.txt). 在

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

Tags: 文件函数texttxtclose成员openstart
1条回答
网友
1楼 · 发布于 2024-09-24 12:29:41

您需要调用函数:

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

相关问题 更多 >