-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathreading_files.py
More file actions
90 lines (39 loc) · 1.39 KB
/
Copy pathreading_files.py
File metadata and controls
90 lines (39 loc) · 1.39 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import os
os.chdir(r"C:\Users\Vidyashree M C\PycharmProjects\Alpha4\files")
# read(no of characters)
with open("sample.txt") as file:
print(file.read()) # reads entire file as a single string
print(file.read(3)) # reads first 3 characters
print(file.read(5)) # read next 5 characters
print(file.read(10))
# readline(no of characters)
with open("sample.txt") as file:
print(file.readline()) # read a single line as a string
print(file.readline())
print(file.readline(5))
print(file.readline(30))
print(file.readline())
# readlines()
with open("sample.txt") as file:
print(file.readlines())
print(file.readlines(10))
# tell() and seek()
with open("sample.txt") as file:
for line in file:
print(line)
print(file.tell())
file.seek(1)
print(file.tell())
for line in file:
print(line)
####################################################################
# write(), writelines()
with open("example.txt", "a") as file:
print(file.write("Today is Monday\n"))
# file.write("Tomorrow is Tuesday")
file.writelines(["Today is Monday\n", "Tomorrow is Tuesday\n"])
# read and write
with open("example.txt", "r+") as file:
file.write("hello\n")
for line in file:
print(line)