Strings Variables Lists Tuples Dictionary Control Function Files Exceptions OOP Algorithm Data Structure back
Flags
flag | what it means | what it does |
---|---|---|
“r” | Read | Default value. Opens a file for reading, error if the file does not exist |
“a” | Append | Opens a file for appending, creates the file if it does not exist |
“w” | Write | Opens a file for writing, creates the file if it does not exist |
“x” | Create | Creates the specified file, returns an error if the file exists |
“t” | Text | Default value. Text mode |
“b” | Binary | Binary mode (e.g. images) |
- To open a file for reading
f = open("demofile.txt") f = open("demofile.txt", "rt")
f = open("demofile.txt", "r")
print(f.read())
f = open("<path>\welcome.txt", "r")
print(f.read())
- append content to the file
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())
- overwrite the content
f = open("demofile3.txt", "w")
f.write("Woops! I have deleted the content!")
f.close()
#open and read the file after the overwriting:
f = open("demofile3.txt", "r")
print(f.read())
# Create a file called "myfile.txt":
f = open("myfile.txt", "x")
#Create a new file if it does not exist:
f = open("myfile.txt", "w")
# remove a file
import os
os.remove("demofile.txt")
# Check if file exists, then delete it:
import os
if os.path.exists("demofile.txt"):
os.remove("demofile.txt")
else:
print("The file does not exist")
# To delete an entire folder
import os
os.rmdir("myfolder")