友元函式及運算子
阿新 • • 發佈:2022-04-05
Read and write files import os os.path.join('str1','str2','str3'...) #return a string connected by '\\' os.getcwd() #return current working directory os.chdir(path) #change cwd to path,path always a directory os.makedirs(path) #create a new folder though there have many intermediate folder os.path.abspath(path) #return the string of the absolute path to parameter os.path.isabs(path) #return True if parameter is a absolute path ,or return False os.path.relpath(path,start) #return a string of relative path from start to path,cwd will be start default os.path.dirname(path) #return a string of all contents before last '\' in path os.path.basename(path) #return a string of all contents after last '\' in path os.path.split(path) #return a tuple including dirname and basename of a path path.split(os.path.sep) #return a list including all parts of the path os.path.getsize(path) #return the number of bytes of file in path os.listdir(path) #return a list of filename including every file in path os.path.exists(path) #return True if file or folder exists in the path os.path.isfile(path) #return True if path exists and it is a file os.path.isdir(path) #return True if path exists and it is a folder File=open(path,mode,encoding) #return a File object File.read() #return a string of file content File.readlines() #return a list of string separated with \n in the file File.write(obj) #return number of characters File.close() import shelve shelfFile=shelve.open(filename) shelfFile[key]=value #save data to binary shelf file,similar to dict list(shelfFile.keys()) list(shelfFile.values()) shelfFile.close() shutil module (shell tool) import shutil shutil.copy(source,destination) #copy the file in source to the folder in destination,if destination is a filename,it will be the new name of copied file.Function return the path of copied file shutil.copytree(sourece,destination) #copy whole folder,return the new path of copied folder shutil.move(source,destination) #move folder or file in the source to destination,return the a string of the new place of absolute path,if a same name file already exists,it will be covered.Furthermore,if destination doesn't exist ,its name will be changed into destination.Original name will be changed if destination specifies a filename. os.unlink(path) #delete the file in path os.rmdir(path) #delete the folder in path ,the folder must be empty shutil.rmtree(path)#delete the folder in path import sen2trash send2trash.send2trash(filename or foldername) #it will deliver file to dustbin but not delete them forever e.g.: for filename in os.listdir(): if filename.endswith('.txt'): #os.unlink(filename) print(filename) for foldername,subfolders,filenames in os.walk(path): #return 3 values: 1.a string of cwd 2.a list of strings for subfolders in cwd 3.a list of strings for files in cwd import zipfile ZipFile=zipfile.ZipFile(filename.zip) ZipFile.namelist() #return a list of strings for all files and folders in the zipfile ZipInfo=ZipFile.getinfo() #return a ZipInfo object ZipInfo.file_size #the size of original file ZipInfo.compress_size #the size of file after compression ZipInfo.file_size/ZipInfo.compress_size is the compression efficiency ZipFile.extractall(path) #compress all files and folders,put them into path,default is cwd ZipFile.extra(filename,path) #compress file and put it into path,return the absolute path of compressed file newZip=zipfile.ZipFile('zipfilename','w') #can use 'a' to add new file to zipfile newZip.write('filename',compress_type=zipfile.ZIP_DEFLATED) #default compression way import csv with open(csvfile,mode,encoding,newline=None) as fp csv.writer(fp) csv.writer().writerow() csv.writer().writerows() #only one parameter csv.DictWriter(fp,key) csv.writer.writeheader() #input key of dict csv.writer().writerow() #input value of dict import json JSON can't store every Python value,and specified object of python wouldn't express json.loads(json string) #convert strings containing JSON data to Python values json.dumps(python value) #convert python values to strings containing JSON data |