-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathw3school_pythonfilehandling.py
More file actions
70 lines (56 loc) · 1.51 KB
/
w3school_pythonfilehandling.py
File metadata and controls
70 lines (56 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Python File Open
# Python has several functions for creating, reading, updating, and deleting files.
# File handling
# The key function for working with files in Python is the open() function.
# The open()function takes two parameters; filename, and mode.
# "r" - Read
# "a" - Append
# "w" - Write
# "x" - Create
# In addition, you can specify if the file should be handled as binary or text mode
# "t" - Text
# "b" - Binary
# Syntax
"""
f = open("demofile.txt", "rt")
print(f.read())
"""
# Close files
# It is a good practice to always close the file when you are done with it
# E.g. Close the file when you are finished with it:
"""
f = open("demofile.txt", "rt")
print(f.read())
f.close()
"""
# Write to an existing file
# E.g. Open the file "demofile2.txt" and append content to the file:
"""
f = open("demofile.txt", "a")
f.write("Now the file has more content!")
f.close()
"""
# Open and read the file after the appending:
"""
f = open("demofile.txt", "r")
print(f.read())
"""
# Create a new file
# To create a new file in Python, use the open() method, with one of the following methods:
# "x" - Create
# "a" - Append
# "w" - Overrite
# E.g. Create a file called "myfile.txt"
"""
f = open("myfile.txt", "x")
"""
# Python Delete File
# To delete a file, you must import the OS module, and run its os.remove() function:
# E.g. Remove the file "demofile.txt"
"""
import os
if os.path.exists("demofile.txt"):
os.remove("demofile.txt")
else:
print("The file does not exist")
"""