Chapter folders renamed
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
This is a sample file 1
|
||||
This is a test data 1
|
||||
@@ -0,0 +1,2 @@
|
||||
This is a sample file 2
|
||||
This is a test data 2
|
||||
@@ -0,0 +1,9 @@
|
||||
#contextmgr1.py
|
||||
with open("myfile.txt",'w') as f1:
|
||||
f1.write("This is a sample file\n")
|
||||
lines = ["This is a test data\n", "in two lines\n"]
|
||||
f1.writelines(lines)
|
||||
|
||||
with open("myfile.txt",'r') as f2:
|
||||
for line in f2.readlines():
|
||||
print(line)
|
||||
@@ -0,0 +1,5 @@
|
||||
#multifilesread1.py
|
||||
|
||||
with open("1.txt") as file1, open("2.txt") as file2:
|
||||
print(file2.readline())
|
||||
print(file1.readline())
|
||||
@@ -0,0 +1,5 @@
|
||||
#multifilesread2.py
|
||||
|
||||
with open("1.txt",'r') as file1, open("3.txt",'w') as file2:
|
||||
for line in file1.readlines():
|
||||
file2.write(line)
|
||||
@@ -0,0 +1,6 @@
|
||||
#multifilesread2.py
|
||||
import fileinput
|
||||
with fileinput.input(files = ("1.txt",'2.txt') )as f:
|
||||
for line in f:
|
||||
print(f.filename())
|
||||
print(line)
|
||||
@@ -0,0 +1,3 @@
|
||||
This is a sample file
|
||||
This is a test data
|
||||
in two lines
|
||||
@@ -0,0 +1,16 @@
|
||||
#writereadfile.pywrite to a file and then read from it
|
||||
f1 = open("myfile.txt",'w')
|
||||
f1.write("This is a sample file\n")
|
||||
lines =["This is a test data\n", "in two lines\n"]
|
||||
f1.writelines(lines)
|
||||
f1.close()
|
||||
|
||||
f2 = open("myfile.txt",'r')
|
||||
print(f2.read(4))
|
||||
print(f2.readline())
|
||||
print(f2.readline())
|
||||
|
||||
f2.seek(0)
|
||||
for line in f2.readlines():
|
||||
print(line)
|
||||
f2.close()
|
||||
Reference in New Issue
Block a user