File read and write
#read the contents of the disk
file=open('a.txt','r')
print(file.readlines()) #put information into a list
file.close()
#write data
file=open('b.txt','w')
file.write('Python') #write data
file.close()
#additional data
file=open('b.txt','a')
file.write('Python') #additional data
file.close()
#binary files for reading and writing
src_file=open('logo.png','rb')
target_file=open('copylogo.png','wb')
target_file.write(src_file.read())
target_file.close()
src_file.close()
Common methods for file objects
#python reading files for
file=open('a.txt','r')
#print(file.read(2)) #read the content of two lines
#print(file.readline()) #get the content of a line
print(file.readlines()) #get content from multiple lines
file.close()
#python writing files to
file=open('c.txt','a')
#file.write('hello')
lst=['java','go','python']
file.writelines(lst)
file.close()
# python the position of the pointer returns data
file=open('c.txt','r')
file.seek(2) #read data based on the position of the pointer
print(file.read())
print(file.tell()) #the data returned by the pointer
file.close()
#write the contents of the buffer
file=open('d.txt','a')
file.write('hello')
file.flush() #write the buffered content to a file but do not close it
file.write('world')
file.close()
#with no need to manually use close(),
with open('a.txt','r') as file:
print(file.read())
#copying images
with open('logo.png','rb') as src_file:
with open('copy2logo.png','wb') as target_file:
target_file.write(src_file.read())
#case
'''
MyContentMgr implemented special methods __enter__(),__exit__() this type of object is said to comply with the context manager protocol
the instance object of this type of object is called the context manager
MyContentMgr()
'''
class MyContentMgr(object):
def __enter__(self):
print('enter the method was called and executed')
return self
def __exit__(self, exc_type, exc_val, exc_tb):
print('exit the method was called and executed')
def show(self):
print('show the method was called and executed',1/0)
with MyContentMgr() as file: #equivalent to file=MyContentMgr()
file.show()
Common functions in the OS module
#os a module related to the operating system
import os
#os.system('notepad.exe')
#os.system('calc.exe')
#directly calling executable files
os.startfile('C:\Program Files\TencentQQ\Bin\qq.exe')
import os
print(os.getcwd()) #return to the current working directory
lst=os.listdir('../chap15') #return the specified directory and files
print(lst)
#os.mkdir('newdir2') #create directory
#os.makedirs('A/B/C') #create multi-level directories
#os.rmdir('newdir2') #delete directory
#os.removedirs('A/B/C')#delete multi-level directories
os.chdir('E:\vippython\chap15') #take path set as current directory
print(os.getcwd())
# os.path module usage
import os.path
print(os.path.abspath('demo13.py')) #obtain the absolute path of directories and files
print(os.path.exists('demo13.py'),os.path.exists('demo18.py')) #check if the file path exists
by true , does not exist false
print(os.path.join('E:\Python','demo13.py')) #concatenate the names of directories and directory files
print(os.path.split('E:\vipython\chap15\demo13.py')) #separate file names and extensions
print(os.path.splitext('demo13.py')) #separate file and directory names
print(os.path.basename('E:\vippython\chap15\demo13.py')) #extract file names from a directory
print(os.path.dirname('E:\vippython\chap15\demo13.py')) #obtain file path from a path, excluding file name
print(os.path.isdir('E:\vippython\chap15\demo13.py')) #determine if it is a path
Common methods for the os.path module