December 2018
Beginner to intermediate
796 pages
19h 54m
English
Opening a file in Python is very simple and intuitive. In fact, we just need to use the open function. Let's see a quick example:
# files/open_try.pyfh = open('fear.txt', 'rt') # r: read, t: textfor line in fh.readlines(): print(line.strip()) # remove whitespace and printfh.close()
The previous code is very simple. We call open, passing the filename, and telling open that we want to read it in text mode. There is no path information before the filename; therefore, open will assume the file is in the same folder the script is run from. This means that if we run this script from outside the files folder, then fear.txt won't be found.
Once the file has been opened, we obtain a file object back, fh, which we can use to work on the ...
Read now
Unlock full access