Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Basic] Files
#1
Files

The built-in function open() creates a Python file object, which serves as a link to a file residing on your machine. After calling open(), you can transfer strings of data to and from the associated external file by calling the returned file objects's methods. 

f = open('test.txt' ,'w')
f.write('some text')
f.close()
This is the basic layout of creating a file. In this example, f is the file object from the builtin method open(). 'test.txt' is the name of the file we are creating and 'w' is the mode we are using (w = write).And finally we close the stream to the file with f.close(). 

f = open('test.txt')
filer = f.read()
f.close()
print(filer)
In this example we are reading the previous example's file. We create the object f with open(). 'test.txt' is the file was are reading. This time there is no mode as open() without a mode default's to read, which you can put 'r' (r = read). f.read() takes in the entire file and assigns it to filer. Then we close the stream to the file and print the variable filer.

Modes
In the above example we used 'w' mode to write to the file. There are however different modes we can use. 'w' is write to. 'r' is read, which by having nothing as mode defaults to 'r'. 'a' is append to the file. 'rb' is read bytes. 'wb' is write bytes. Adding a '+' to either the input or output mode you can read and write to the same file object. 

File Object Methods
file.readlines()
f.readlines() will read the entire file with each line as a index of a list. The first line being the first index and so on. 
f = open('test.txt' ,'w')
f.write('from the first line \nfrom the second line \nfrom the third line')
f.close()

f = open('test.txt')
filer = f.readlines()
f.close()
print(filer)
You can then if needed strip off the newline with the builtin function strip() upon iterating the list. 
['from the first line \n', 'from the second line \n', 'from the third line']
file.readline()
f.readline() will read the next line into the string including the newline character. 
f = open('test.txt' ,'w')
f.write('from the first line \nfrom the second line \nfrom the third line')
f.close()


def next_line(f):
    return f.readline()
    
f = open('test.txt')

print(next_line(f))
print('a')
print(next_line(f))
print('b')
print(next_line(f))
print('c')
The output below shows each line being called along with its newline character which is why there is a blank space. Then after each line we have out regular print statement. The c is not printed after a newline because the thrid line in the file is the last line, which does not have a newline in it, thus there is not newline printed between the two. 
m the first line 

a
from the second line 

b
from the third line
c
file.read()
f.read() will take the enitre file into a string, separating each line with '\n' (the newline). 
>>> f = open('test.txt', 'w')
>>> f.write('some random data\nother random data')
34
>>> f.close()

>>> f = open('test.txt')
>>> f.read()
'some random data\nother random data'
Instead of this:
>>> f = open('test.txt', 'w')
>>> f.write('some random data\nother random data')
34
>>> f.close()
it is much more advisable to do it like this:

>>> with open('test.txt', 'w') as f:
...     f.write('some random data\nother random data')
... 
34
You'll notice that I didn't have to explicitly say "f.close()" here.  That's because, if I use the "with open" construct, Python closes it for me whenever I leave that indented block of code.  This is not only more convenient, but it's also safer, because it means that if an exception is raised while reading from or writing to the file, Python will automatically close the file before continuing, whereas if you do it the other way, the file could be left open in the event of an exception being raised.
Recommended Tutorials:
#2
No mention of context managers?
#3
Feel free to modify it. I just copied it over from the last forum. And i wrote it a few years ago.
Recommended Tutorials:
#4
Was mostly looking for something productive to say while removing this thread from Unanswered =p
I might get to it, but don't expect to for at least a few weeks. Anyone else is welcome to though =D

If anyone wants to write a reply to this with improvements, a moderator can merge it into the OP.
#5
Quote:Was mostly looking for something productive to say while removing this thread from Unanswered =p
oh  :D

Quote:If anyone wants to write a reply to this with improvements, a moderator can merge it into the OP.
as well as any other tutorial.
Recommended Tutorials:


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020