python class finishing 25 ---- os and sys module

A, os module

1. Get current working directory

import the 
print (os.getcwd ())

2. Change the current working path, the equivalent of cd under the shell

import os
os.chdir(r"D:\programme\untitled\Day1\my_module")
print(os.getcwd())

3. Return the current directory os.curdir == "."

import os 
os.chdir (os.curdir) 
print (os.getcwd ()) 
os.chdir ( ".") 
print (os.getcwd ())

4. Return the parent directory os.pardir == ".."

import the 
os.chdir (os.pardir) 
print (os.getcwd ()) 
os.chdir ( "..") 
print (os.getcwd ())

5. Generate multilayer recursive directory

import os
os.chdir(r"D:\programme\untitled\Day1\my_module")
os.makedirs("three/four/five")

6. delete a directory, if the directory is empty, then remove and recursively to the parent directory, if also is empty, delete, and so on

import os
os.removedirs(r"D:\programme\untitled\Day1\my_module\three\four\five")

7. The production of single-stage empty directory

import os
os.chdir(r"D:\programme\untitled\Day1\my_module\three")
os.mkdir("single")

8. Remove the single-level directory, if the directory is not empty, you can not delete, an error

os Import 
os.rmdir (r "D: \ Program \ Untitled \ Day1 \ my_module \ Three \ single") # delete the above new single directory

9. List all files and subdirectories in the specified directory, including hidden files, and printed in list form

import os
print(os.listdir(r"D:\programme\untitled\Day1"))

10. Delete a file

os Import 
os.remove (r "D: \ Program \ Untitled \ Day1 \ my_module \ __ init__.py") # __init__.py file directly under my_module to delete

11. Rename the file

os Import 
os.rename ( "old_name", "new_name") # parameters can also be two absolute paths

12. Get file / directory information

atime: last visit user time, size: size (bytes), mtime: the modification time user, ctime: Created

import os
print(os.stat("syudy1.py"))

13. os. Sep output operating system specific path delimiter, the "\", is under the linux win "/"

import the 
print (os.sep)

14. os.linesep output current line terminator platform (the line feed) for the next "\ r \ n" linux for the next win "\ n" 

15. os.pathsep outputs a string into a file path, win is lower; under linux is: 

16. os.system ( "bash command") to run shell commands

17. os.environ acquisition system environment variables

18. os. Path.split (path) into the path name of the directory and file return tuple

import os
print(os.path.split(r"D:\programme\untitled\Day1\syudy1.py"))

19. os. Path.dirname (path) Returns the path of the directory, a directory may be used to obtain the

import os
a1 = os.path.dirname(r"D:\programme\untitled\Day1\syudy1.py")
print(a1)
a2 = os.path.dirname(a1)
print(a2)
a3 = os.path.dirname(a2)
print(a3)

20. os. Path.baseanme (path) Returns the last path of the file name, if the path to / \ or ends, then return null

import os
print(os.path.basename(r"D:\programme\untitled\Day1\syudy1.py"))

21. os.path.exists (path) path, if present, returns True, if the path does not exist, returns False

22. os.path.isfile (path) if the path is a file, returns True, otherwise False

23. os.path.isdir (path) if the path is an existing directory, it returns True, otherwise False

♦ 24. Stitching path

import os
a = r"D:\programme\untitled\Day1"
b = "syudy1.py"
print(os.path.join(a, b))

25. os. Path. Getatime (path) Returns the last access time path points to a file or directory

♦ 26. Os.path.getmtime (path) Returns the last modification time path points to a file or directory

Two, sys module

1. sys.exit (n) to exit the program, when the normal exit exit (0)

2. sys.path return module search path

import sys
print(sys.path)

3. sys.platform return to the operating system platform name

♦ 4. Sys.argv or performed at the terminal py file, the contents can be entered at the back, stored in a list, the following can be brought by

 

5. A progress bar sys. Stdout.write ( "#")

import sys
import time
for i in range(100):
    sys.stdout.write("#")
    time.sleep(0.1)
    sys. stdout.flush()  #刷新,不刷新的话,它会把100个# 全加载到内存后,一次性打印出来

  

Guess you like

Origin www.cnblogs.com/dabai123/p/11330905.html