Python learning (18) OS module method

import OS 
print(os.getcwd()) #Get the current working directory

os.chmod('/usr/local',7) #Add permissions to files/directories in
Linux print(os.chdir("../day5") ) #Change current directory, relative path
print(os.chdir("F:\pycharm_workspace\codebyemily\day5")) #Change current directory, absolute path
print(os.getcwd())
print(os.makedirs('liujia/ test'))#Create a folder recursively, create a parent directory when the parent directory does not exist
print(os.mkdir('liujia1/test1'))#Create a folder, if there is no parent directory, an error will be reported
print(os.removedirs('liujia/ test'))#Recursively delete empty directories
print(os.rmdir('test1')) #Delete the specified folder, and only empty directories can be deleted
print(os.remove('liujia/test/11')) #Only Can delete files
print(os.rmdir('test')) #Can only delete folders
print(os.listdir('G:\\')) #List all files in a directory
print(os.rename(' test','test1'))
#rename print(os.stat('test.txt'))#Get file information
print(os.sep) #The path separator of the current operating system, under Windows is \
#Writing in this way, it can be used on different systems at the same time, and no error will be reported
print(os.linesep) #The newline character of the current operating system\n,\r
print(os.pathsep) #The separator of the current system environment variable , linux is: windows is;
print(os.environ)#environment variables of the current system
print(os.name) #the name of the current system, the windows system is nt linux system is posix
os.system('ipconfig') #execute the operation For system commands,
res=os.popen('ipconfig').read()# can get the result of command execution
print(res)

print(os.path.abspath(__file__)) #Get the absolute path 
print(__file__) #Get the absolute path of the current file
print(os.path.dirname("G:\360Downloads\Software")) #Get the parent directory, Get its parent directory
print(os.path.basename('G:\360Downloads\wpcache\360wallpaper.jpg')) #Get the last level
print(os.path.exists('')) #Whether the directory exists
print(os.path.isabs('../day5')) #Judging whether it is an absolute path
print(os.path.isfile('ceshi.py')) #Judging whether it exists and whether it is a file
print(os .path.isdir('')) #Whether it is a path, whether the directory exists
size=os.path.getsize('') ​​#Get the size of the file
print(size)
print(os.path.join('root','hehe','mysql','a.sql')) #Splice a path
 
for abs_path,dir,file in os.walk( ' ../day6 ' ):
     print (abs_path,dir,file)
 #abs_pathThe absolute path of the current loop #All 
folders under the dir directory #All files 
under the file directory
 
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325167278&siteId=291194637