-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPython File Handling.py
More file actions
39 lines (31 loc) · 924 Bytes
/
Python File Handling.py
File metadata and controls
39 lines (31 loc) · 924 Bytes
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
# File Handling in Python
try:
# Open file in current directory
fName = open("words.txt", mode ='r')
# Open a file on a different location
fhand = open("E:\\files\python.txt", mode='r')
# This will print every line one by one in the file
for line in fName :
line = line.rstrip() # remove new
print(line)
# Read from file
fhand .read() # read all the file
fName.read(2) # read the first 2 data
fName.read(3) # read the next 4
# Read Lines from file
print(fName.readline())
print(fName.readline())
# Create file in write() mode
newfile = open("texz.txt", mode ='r')
newfile .write("let write to file")
newfile .write("let wirte more and more")
# Write to an Existing File
file = open("E:\\files\python.txt", mode ='a')
file.write("add new word ")
except:
print("file cant open")
quit()
# Closing File
fhand.close()
fName.close()
file.close()