用Python读写文件 [英] Reading and Writing files in Python

查看:82
本文介绍了用Python读写文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 包含名称列表的文件

  2. 一个不同的文件(可以是空的或非空的)


    脚本的目的是取名从第一个文件访问某个API并获取每个名称的信息,然后将该信息写入第二个文件。如果第二个文件已经包含信息,则脚本读取写入文件的最后一行,在第一个文件中找到该条目,然后从该条目开始。我遇到了麻烦,但是,当涉及到读取和写入到第二个文件。

    当我将第二个文件类型设置为r +,然后做一个打印在file_two.read()上,它显示了第二个文件的内容。当我在下一行做另一个打印时,它显示文件为空。同样,当我将文件类型设置为a +时,即使我可以清楚地看到文本文件中的信息确实存在,它也显示文件完全是空的。任何人都知道发生了什么事情?



    以下是处理写作的代码段:

      def write_to_file(filename,users):
    pages = range(0,len(users) - 100,100)
    页面中的页面:
    user_names = get_users(users [
    output_text ='\ n'.join(lines)
    with filename作为output_file:
    $ b lines = [user_user中用户的格式用户$ b output_file.write(output_text.encode('utf-8'))

    这里的代码

      file_one = args.file1 
    file_two = args.file2
    用户= read_names_automatic(file_one)
    write_to_file(file_two,users)


    解决方案


    将第二个文件类型设置为r +,然后在file_two.read()上执行打印时,将显示第二个文件的内容。当我在下一行做另一个打印时,它显示文件为空。

    是的,这是预期的行为。文件指针指向 file_two.read()结尾的文件末尾,所以任何进一步读取的尝试都会返回一个空字符串。例如,请查看以下内容:

      with open(test.txt)as f:
    print f .readline()#=>打印第一行,将文件指针设置为第二行
    print f.read()#=>从
    的第二行打印所有内容

    重置文件指针到文件的开头: print f.read()#=>

     打印整个文件,将文件指针设置为文件结尾
    f.seek(0)#=>重置文件指针到文件开头
    print f.read()#=>打印整个文件




    当我将文件类型设置为即使我可以清楚地看到文本文件中的信息确实存在。




    <当你 open(test.txt,a +)时,文件指针被设置为文件末尾,所以任何尝试读取都会产生一个空的字符串。事实上,这是允许您将追加到文件。如果你 file_two.seek(0)然后 file_two.write(),写入的文本将被添加到 file_two


    I'm writing a script that has two inputs:

    1. a file containing a list of names,
    2. a different file (can be empty or non-empty)

    The purpose of the script is to take the names from the first file, access a certain API and get information on each name, then write that information to the second file. If the second file already contains information, the script reads the last line of the written file, finds that entry in the first file, and then starts from that entry. I'm running into trouble however, when it comes to reading and writing to the second file.

    When I set the second file type to "r+" and then do a print on file_two.read(), it shows the contents of the second file. When I do another print on the very next line, it shows the file as empty. As well, when I set the file type to "a+", it shows the file as completely empty both times, even though I can clearly see the information in the text file is indeed there. Anyone know what's going on?

    Here's the code segment that handles the writing:

    def write_to_file(filename, users):
        pages = range(0, len(users) - 100, 100)
        for page in pages:
            user_names = get_users(users[page: page+100])
            lines = [format_user(user) for user in user_names]
            output_text = '\n'.join(lines)
            with filename as output_file:
                output_file.write(output_text.encode('utf-8'))
    

    and here's the code segment that calls the above function.

    file_one = args.file1
    file_two = args.file2
    users = read_names_automatic(file_one)
    write_to_file(file_two, users)
    

    解决方案

    When I set the second file type to "r+" and then do a print on file_two.read(), it shows the contents of the second file. When I do another print on the very next line, it shows the file as empty.

    Yes, this is expected behavior. The file pointer is pointing to the end of the file at the end of file_two.read(), so any attempt to read further returns an empty string. For example, check out the following:

    with open("test.txt") as f:
        print f.readline() # => prints the first line, sets file pointer to second line
        print f.read() # => prints everything from the second line on
    

    To reset the file pointer to the beginning of the file:

    with open("test.txt") as f:
        print f.read() # => prints entire file, sets file pointer to end of file
        f.seek(0) # => reset file pointer to beginning of file
        print f.read() # => prints entire file
    

    As well, when I set the file type to "a+", it shows the file as completely empty both times, even though I can clearly see the information in the text file is indeed there.

    When you open("test.txt", "a+"), the file pointer is set to the end of the file, so any attempt to read will yield an empty string. Indeed, this is what allows you to append to the file. If you file_two.seek(0) and then file_two.write(), the written text will be added to the beginning of file_two.

    这篇关于用Python读写文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆