简体   繁体   中英

Appending lines to a file, then reading them

I want to append or write multiple lines to a file. I believe the following code appends one line:

with open(file_path,'a') as file:
    file.write('1')

My first question is that if I do this:

with open(file_path,'a') as file:
    file.write('1')
    file.write('2')
    file.write('3')

Will it create a file with the following content?

1
2
3  

Second question—if I later do:

with open(file_path,'r') as file:
    first = file.read()
    second = file.read()
    third = file.read()

Will that read the content to the variables so that first will be 1 , second will be 2 etc? If not, how do I do it?

The answer to the first question is no. You're writing individual characters. You would have to read them out individually.

Also, note that file.read() returns the full contents of the file.

If you wrote individual characters and you want to read individual characters, process the result of file.read() as a string.

text = open(file_path).read()
first = text[0]
second = text[1]
third = text[2]

As for the second question, you should write newline characters, '\\n' , to terminate each line that you write to the file.

with open(file_path, 'w') as out_file:
  out_file.write('1\n')
  out_file.write('2\n')
  out_file.write('3\n')

To read the lines, you can use file.readlines() .

lines = open(file_path).readlines()
first = lines[0]   # -> '1\n'
second = lines[1]  # -> '2\n'
third = lines[2]   # -> '3\n'

If you want to get rid of the newline character at the end of each line, use strip() , which discards all whitespace before and after a string. For example:

first = lines[0].strip()   # -> '1'

Better yet, you can use map to apply strip() to every line.

lines = list(map(str.strip, open(file_path).readlines()))
first = lines[0]   # -> '1'
second = lines[1]  # -> '2'
third = lines[2]   # -> '3'

Question 1: No.

file.write simple writes whatever you pass to it to the position of the pointer in the file. file.write("Hello "); file.write("World!") file.write("Hello "); file.write("World!") will produce a file with contents "Hello World!"

You can write a whole line either by appending a newline character ( "\\n" ) to each string to be written, or by using the print function's file keyword argument (which I find to be a bit cleaner)

with open(file_path, 'a') as f:
    print('1', file=f)
    print('2', file=f)
    print('3', file=f)

NB print to file doesn't always add a newline, but print itself does by default! print('1', file=f, end='') is identical to f.write('1')

Question 2: No.

file.read() reads the whole file, not one line at a time. In this case you'll get

first == "1\n2\n3"
second == ""
third == ""

This is because after the first call to file.read() , the pointer is set to the end of the file. Subsequent calls try to read from the pointer to the end of the file. Since they're in the same spot, you get an empty string. A better way to do this would be:

with open(file_path, 'r') as f:  # `file` is a bad variable name since it shadows the class
    lines = f.readlines()
    first = lines[0]
    second = lines[1]
    third = lines[2]

Or:

with open(file_path, 'r') as f:
    first, second, third = f.readlines()  # fails if there aren't exactly 3 lines

Writing multiple lines to a file

This will depend on how the data is stored. For writing individual values, your current example is:

with open(file_path,'a') as file:
    file.write('1')
    file.write('2')
    file.write('3')

The file will contain the following:

123

It will also contain whatever contents it had previously since it was opened to append. To write newlines, you must explicitly add these or use writelines() , which expects an iterable.

Also, I don't recommend using file as an object name since it is a keyword, so I will use f from here on out.

For instance, here is an example where you have a list of values that you write using write() and explicit newline characters:

my_values = ['1', '2', '3']

with open(file_path,'a') as f:
    for value in my_values:
        f.write(value + '\n')

But a better way would be to use writelines() . To add newlines, you could join them with a list comprehension:

my_values = ['1', '2', '3']

with open(file_path,'a') as f:
    f.writelines([value + '\n' for value in my_values])

If you are looking for printing a range of numbers, you could use a for loop with range (or xrange if using Python 2.x and printing a lot of numbers).

Reading individual lines from a file

To read individual lines from a file, you can also use a for loop:

my_list = []

with open(file_path,'r') as f:
    for line in f:
        my_list.append(line.strip())  # strip out newline characters

This way you can iterate through the lines of the file returned with a for loop (or just process them as you read them, particularly if it's a large file).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM