The Road to Python (Part 14) os module

 

1. os module

1、os.getcwd()

Get the current working directory (the current working directory is the folder where the current file is located by default)

  import them
  print(os.getcwd())

  

 

2、os.chdir(path)

Change the current working directory, the os.chdir() parameter is the path

  
  import them
  print(os.getcwd())
  print(os.chdir(r"d:\python\exercise\test_package\a"))
  print(os.getcwd())

  

output result

  D:\06python\exercise\test_package
  None
  d:\06python\exercise\test_package\a #You can see that the working path has changed

  

 

3、os.curdir os.pardir

os.curdir Returns the current directory: ('.') os.pardir Gets the parent directory string name of the current directory: ('..')

  import them
  print(os.curdir)
  print(os.pardir)

  

output result

  
  .
  ..

  

 

4、os.makedirs("dirname1/dirname2")

Can generate multi-level recursive directories

  
  import them
  os.makedirs(r"exercise\a\b\c")

  

output result

 

 

 

5、os.removedirs(”dirname1“)

Recursively delete folders must be empty directories. If the directory is empty, delete it and recurse to the previous directory. If it is also empty, delete it, and so on.

  
  import them
  os.removedirs(r"exercise\a\b\c")

  

output result

 

 

 

6、os.mkdir('dirname')

Generate a single-level directory; equivalent to mkdir dirname in the shell

 

  
  import them
  os.mkdir("a")

  

output result

 

 

 

7、os.rmdir('dirname')

Delete a single-level empty directory. If the directory is not empty, it cannot be deleted, and an error is reported; it is equivalent to rmdir dirname in the shell

  
  import them
  os.rmdir("a")
  # os.rmdir("b")

  

output result

 

 

 

 

8、os.listdir('dirname')

List all files and subdirectories in the specified directory, including hidden files, and print them in a list

  
  import them
  print(os.listdir("a"))

  

output result

 

 

 

 

9、os.remove()

delete a file

  
  import them
  os.remove("a/x.py")

  

output result

 

 

 

10、os.rename("oldname","newname")

rename file/directory

Use os.rename() under Linux platform, os.rename cannot be used directly under Windows platform, use os.replace() under Windows platform,

  
  import them
  os.rename("test.py","new_test.py")

  

output result

 

 

 

11、os.stat('path/filename')

Get file/directory information

  
  import them
  print(os.stat("test.py"))

  

output result

 

 

 

  • st_mode: inode protection mode

  • st_ino: inode node number.

  • st_dev: The device where the inode resides.

  • st_nlink: The number of links for the inode.

  • st_uid: User ID of the owner.

  • st_gid: The owner's group ID.

  • st_size: Size of normal files in bytes; contains data waiting for some special files.

  • st_atime: The time of the last visit.

  • st_mtime: The time of the last modification.

  • st_ctime: "ctime" as reported by the operating system. On some systems (like Unix) the time of the last metadata change, on others (like Windows) the time of creation.

 

 

12 、 os.sep

Output the OS-specific path separator, " \ " under win, "/" under Linux

  
  import them
  print(os.sep)

  

output result

 

 

 

 

13 os.linesep

Output the line terminator used by the current platform, "\r\n" under win, "\n" under Linux

 

 

14、os.pathsep

The output string used to split the file path is; under win, and under Linux:

 

 

15、os.name

The output string indicates the currently used platform. win->'nt'; Linux->'posix'

 

 

16、os.system("bash command") os.popen("bash command“)

os.system("bash command") run the shell command and display it directly

os.popen("bash command") run the shell command and get the execution result

 

 

17 、 os.environ

Get system environment variables

 

 

18、os.path.abspath(path)

Returns the absolute path normalized by path, including the filename

  
  import them
  print(os.path.abspath(__file__))

  

output result

 

 

 

19、os.path.split(path)

 

When path is the absolute path of the file, split the path into a directory and a filename 2-tuple to return

When path is the absolute path of a file directory (like "D:\06python\exercise\test_package"), os.path.split() splits the path into the last folder as a part, and the other as a part in front

  
  import them
  print(os.path.split(__file__))

  

output result

 

 

 

 

Example 2

  
  import them
  print(os.path.split(r"D:\06python\exercise\test_package"))

  

output result

 

 

 

20、os.path.dirname(path)

Returns the directory of path. In fact, it is the first element of os.path.split(path).

When the parameter path is a file, os.path.dirname() is the directory part of the complete absolute path to get the file (remove the file name)

When the parameter path is a directory, os.path.dirname() obtains the directory part of the complete path, and obtains the absolute path of the upper-level directory

os.path.dirname(os.path.dirname(path)) is the upper-level directory obtained and returned to the directory, that is, the absolute value of the upper-level directory of the specified directory (the result of os.path.dirname(path)). path

  import them
  print(os.path.abspath(__file__))
  print(os.path.dirname(__file__))
  print(os.path.dirname(os.path.dirname(__file__)))

  

 

output result

 

 

Analysis: The last two output results here are slashes, and the first os.path.abspath output is the common backslashes in the win platform, the reason is the optimization done by pycharm

small experiment

 

 

 

As can be seen from this result, the result of using the python interpreter directly is still backslashes

The same file is executed in pycharm, and the output in pycharm is the slash

 

 

21、os.path.basename(path)

When the parameter path is the absolute path of the file, it returns the last file name of path. If path ends with / or \, then it will return a null value. That is, the second element of os.path.split(path).

When the parameter path is an absolute path of a file directory (like "D:\06python\exercise\test_package"), the name of the last folder is returned

 

 

 

 

 

22、os.path.exists(path)

Determine whether the file or directory exists, if the path exists, return True; if the path does not exist, return False

 

 

 

 

 

23、os.path.isabs(path)

Return True if path is an absolute path

  
  import them
  print(os.path.isabs(r"D:\06python\exercise\test_package\test.py"))
  print(os.path.isabs(r".\test_package\test.py"))
  print(os.path.isabs(__file__))
  v =os.path.isabs(r"D:\06python\exercise\test_package")
  print(v)
  print(type(v))

  

output result

 

 

 

Analysis: Here even an absolute path that does not exist will return True

 

 

24、os.path.isfile(path)

Returns True if path is an existing file. Otherwise return False

  
  import them
  print(os.path.isfile(r"D:\06python\exercise\test_package\test.py"))
  print(os.path.isfile(r"D:\06python\exercise\test_package\t.py"))
  v =os.path.isfile(r"D:\06python\exercise\test_package")
  print(v)
  print(type(v))

  

output result

 

 

 

 

25、os.path.isdir(path)

Returns True if path is an existing directory. Otherwise return False

 

 

 

 

26、os.path.join(path1[, path2[, ...]])

Return after combining multiple paths, parameters before the first absolute path will be ignored

  import them
  print(os.path.join(r"D:\06python\exercise",r"test_package\test.py"))
  print(os.path.join(r"D:\06python\exercise",r"test_package\t.py")) #Paths that do not exist can also be synthesized
  print(os.path.join(r"D:\06python\exercise",r"/test_package\t.py"))
  """If there is a slash or backslash in the second parameter, the previous parameter will be ignored i.e. the parameter before the first absolute path will be ignored"""
  print(os.path.join(r"D:\06python\exercise","test_package"))

  

output result

 

 

 

 

27、os.path.getatime(path)os.path.getmtime(path)

os.path.getatime(path) Returns the last access time of the file or directory pointed to by path os.path.getmtime(path) Returns the last modification time of the file or directory pointed to by path

  
  import them
  print(os.path.getatime(__file__)) # Returns the last access time of the file or directory pointed to by path
  print(os.path.getmtime(__file__)) # Returns the last modification time of the file or directory pointed to by path
  print(os.path.getatime(r"D:\06python\exercise\test_package"))
  print(os.path.getmtime(r"D:\06python\exercise\test_package"))

  

output result

 

 

 

 

 

28、os.path.getsize(path)

Returns the size of path (file or all files in a directory)

  
  import them
  print(os.path.getsize(__file__))
  print(os.path.getsize(r"D:\06python\exercise\test_package"))

  

output result

 

 

Guess you like

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