Files can be read and written into with python.
Plaintext files is just a text file without any information about the font, size or colour. Python files are also example of plaintext files except they have .py file extension.
The other type of file are binary files. These are every other type of file, like pdf, images, exe, etc
There are three steps to reading and writing file in python. First with open() function.
open() will read the file in plaintext read mode. With read mode, we can only read and not modify the file. Read mode is the default mode.
open('F:\\Example\\something.txt')
newFile = open('F:\\Example\\something.txt')
newFile.read()
newFile.close()
newFile.read()
If we want to read it again, we would have to open the file again.
newFile = open('F:\\Example\\something.txt')
content = newFile.read()
print(content)
newFile.close()
newFile = open('F:\\Example\\something.txt')
newFile.readlines()
newFile.close()
Content of the file can be changed if the file is opened in write mode. Write mode will overwrite the file with a blank file and start from scratch
Files can be opened in write mode the following way by passing the second argument w to the open function
newFile = open('F:\\Example\\something.txt', 'w')
Append mode will append the text to the existing file without overwriting the whole file.
Files can be opened in append mode by passing a second argument 'a' to the open function.
newFile = open('F:\\Example\\something.txt', 'a')
In both cases of write mode and append mode, if the file in the path doesn't already exists, python will just create a new blank text file to write to.
newFile = open('F:\\Example\\somenewfile.txt', 'w')
newFile.write('Hello!!!!!!!!!!!!!!!!')
write() method returns how many bytes of character it wrote to it.
newFile.write('Hello!!!!!!!!!!!!!!!!')
newFile.write('Hello!!!!!!!!!!!!!!!!')
newFile.close()
newFile = open('F:\\Example\\somenewfile.txt')
content = newFile.read()
print(content)
so by default, write() does not adds a new line to a new addition made with it.
newFile = open('F:\\Example\\somenewfile.txt')
newFile.write('Hello')
Above is not writable because it is in read mode
newFile = open('F:\\Example\\somenewfile.txt', 'a')
newFile.write('\n\nHello!!!!!') #\n need to be added to the string in write()
newFile.close()
newFile = open('F:\\Example\\somenewfile.txt') #opening in read mode
content = newFile.read()
print(content)
newFile.close()
someFile = open('somefile.txt', 'w') #relative file path
someFile.write('Some text in here')
someFile.close()
import os
os.getcwd()
os.listdir(os.getcwd())
notice the somefile.txt is in the list of files.
someFile = open('somefile.txt', 'a')
someFile.write("This string will be added at the end because it was opened in append mode.")
someFile.close()
read = open('somefile.txt')
content = read.read()
print(content)
someFile.close()
read.close()
Writing and reading a text file is a good way to store single long string. But if we want to store variables that have lists and dictionaries and other complex data structures, we can save variables to binary shelve files using the shelve module
import shelve
shelfFile = shelve.open('mydata') #shelve.open() returns a shelvefile object
Changes to the shelve value can be made as if it were a dictionary. And when done, call the close on the shelf value.
shelfFile['phones'] = ['s9+', 'pixel2', 'iphonex'] #key is phones
shelfFile.close()
shelfFile = shelve.open('mydata')
shelfFile['phones']
shelfFile.close()
os.getcwd()
If I go to above directory, there are three files -
mydata.bak
mydata.dat
mydata.dir
These are the files where the information for the shelve is stored, and they are binary files. i.e. even if the files are opened in notepad, the content will be full of weird characters.
shelfFile = shelve.open('mydata')
shelfFile.keys()
list(shelfFile.keys())
list(shelfFile.values())