file handling

The key function for working with files in Python is the open() function. There are four different ways (modes) to open a file

"r" - read - default value. Open a file for reading, or error if the file does not exist.

"a" - Append - opens a file for appending, creating the file if it does not exist

"w" - write - open a file for writing, create the file if it doesn't exist

"x" - Create - Creates the specified file, returning an error if the file exists.

Additionally, you can specify that the file should be processed in binary or text mode.

"t" - text - default value. text mode

"b" - binary - binary mode (eg image).

To open a file for reading, simply specify the name of the file

f = open("demofile.txt")

The code above is the same as

f = open("demofile.txt""rt")

Since "r" stands for read and "t" stands for text, which are the defaults, you don't need to specify them.

NOTE: Make sure the file exists, otherwise you will get an error.

read file

The open() function returns a file object, which has a read() method for reading the contents of the file

f = open("demofile.txt""r")
print(f.read())

If the file is in a different location you will have to specify the file path like this

f = open("D:\\myfiles\welcome.txt""r")
print(f.read())

Read only part of the file

f = open("demofile.txt""r")
print(f.read(5))

read line

f = open("demofile.txt""r")
print(f.readline())

By calling readline() twice, you can read the first two lines

f = open("demofile.txt""r")
print(f.readline())
print(f.readline())

By iterating over the lines of the file, you can read the entire file line by line

f = open("demofile.txt""r")
for x in f:
  print(x)

It's best to always close the file when you're done processing it.

f = open("demofile.txt""r")
print(f.readline())
f.close()

NOTE: You should always close your file, in some cases changes made to the file may not appear until you close the file due to buffering.

write to file

To write to an existing file, an argument must be added to the open() function

"a" - Append - will append to the end of the file.

"w" - write - will overwrite any existing content

f = open("demofile2.txt""a")
f.write("Now the file has more content!")
f.close()

#open and read the file after the appending:
f = open("demofile2.txt""r")
print(f.read())
f = open("demofile3.txt""w")
f.write("Woops! I have deleted the content!")
f.close()

#open and read the file after the appending:
f = open("demofile3.txt""r")
print(f.read())

Note: The "w" method will overwrite the entire file.

To create a new file in Python, use the open() method with one of the following arguments

"x" - create - will create a file, return an error if the file exists

"a" - Append - will create a file if the specified file does not exist

"w" - write - will create a file if the specified file does not exist

f = open("myfile.txt""w")

Delete Files

To remove a file, you must import the OS module and run its os.remove() function

import os
os.remove("demofile.txt")

Check if file exists

import os
if os.path.exists("demofile.txt"):
  os.remove("demofile.txt")
else:
  print("The file does not exist")

To delete an entire folder, use the os.rmdir() method

import os
os.rmdir("myfolder")

Note: You can only delete empty folders.