-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path11.py
53 lines (46 loc) · 1.63 KB
/
11.py
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
import os
"""
Opening and transparently closing a file
"""
file_path = '/etc/passwd'
print "Does file exist at all?"
if os.path.exists(file_path):
print "File exists"
else:
print "File does not exist"
print "Open a file. Since you use 'with' you don't need to explicitly close it with f.close()"
with open('foo.txt') as f:
print "The file descriptor for this file is", f.fileno()
for line in f:
print line
print "Reading a certain number of bytes from the file"
with open('foo.txt') as f:
s1= f.read(6)
print s1
print "Read all the data from a file - note that the file pointer position is remembered here"
s2=f.read()
print s2
print "Reading a certain number of lines from the file"
with open('foo.txt') as f:
s1= f.readline()
print s1
print "Current file position is", f.tell()
print "Move file position by 7", f.seek(7, os.SEEK_CUR)
print "Read the rest of the lines. Note that the file pointer position is remembered here"
s2= [x for x in f.readlines()]
for i in s2:
print i
print "Read whole file as lines but without the extra newline character that readlines() gives you"
with open('foo.txt') as f:
x= f.read().splitlines()
print x
print
print "Writing a certain number of lines to a file"
with open('blah.txt', 'w') as f:
f.write('abcd')
print "Reduce file size to the number inside the brackets. Here there will be only 1 character remaining."
f.truncate(1)
l1=['a', 'b', 'c']
with open('blah.txt', 'a') as f:
print "Write a sequence of strings to the file. Make sure that you close any previous handles before writing content"
f.writelines(l1)