Python OS module and Pythonos Module

Source: Internet
Author: User

Python OS module and Pythonos Module
Python OS Module 1. OS. getcwd ()

Obtain the current working directory.

ImportOS

Print (OS. getcwd () # I: \ Python Program

Ii. OS. chdir ('path ')

Change the working directory of the current script. It is equivalent to a cd in shell.

Iii. OS. curdir
import os
print (os.curdir)     #.
Iv. OS. pardir
import os
print (os.pardir)     #..
V. OS. makedirs ('path ')
import os
OS. makedirs ('A/B/C') # Recursively create a folder, B folder, and c folder in the current working directory
Vi. OS. removedirs ('path ')
import os
OS. removedirs ('A/B/C') # Delete empty folders a, B, and c. If there are other files in the folder, the folder and its upper-level directories are not deleted.
VII. OS. mkdir (PATH)
import os
OS. mkdir ('A/B/C') # create a c folder under path a/B. It cannot be recursively created like OS. makedirs ().
8. OS. rmdir ('path ')

Import OS

OS. rmdir ('A/B/C') # Delete the empty folder c. When there are other files in the folder, the folder cannot be deleted.
9. a = OS. listdir (r'path ')

List the files and folders in this path.

import os
A = OS. listdir (R'h: \ PythonProgram\ Ceshi01')
print(a)   # ['123.py', '4546.py', 'a', 'hsd.py']
10. OS. remove ()

The specified file cannot be deleted. The delete path can be a relative or absolute path.

import os
OS. remove ('2014. py') # Delete the '127. py' file in the working directory.
11. OS. rename ('old filename ', 'new filename ')

Rename a file or folder

import os
OS. rename ('2014. py','2014. py') # Rename '2017. py' to '2017. py'
12. OS. stat ('path/filename ')

Get File/directory information

import os ,time
Print (OS. stat ('H:/PythonProgram/Ceshi01/hsd. py'))

# OS. stat_result (st_mode = 33206, st_ino = 7036874417766660, st_dev = 321530, st_nlink = 1, st_uid = 0, st_gid = 0, st_size = 274, st_atime = 1509104633, st_mtime = 1509104633, st_ctime= 1509093175)

print(time.ctime(a.st_atime))    
# Fri Oct 27 19:48:37 2017. Change the last access time to ctime format
13. OS. sep

Specific path Separator of the output Operating System

s= os.sep
Print ('H :'+ S +'PythonProgram'+ S +'Ceshi01') # H: \ Python program \ ceshi01
14. OS. linesep

Output the row Terminator used by the current platform. Win is "/r/n", Linux is "/n", and mac is "/r ".

15. OS. pathsep

Output the directory delimiter of the current operating system. For Windows, the Delimiter is ";", and for linux, The Delimiter is ":".

15th, OS. name

The output string indicates that the current platform is used. Windows is "nt" and Linux is "posix ".

16. OS. system ("bash command ")

Run the shell command to display it directly.

Import OS

Print (OS. system ("cmd "))

17. OS. environ

Output the environment variables of the operating system.

18. OS. path. abspath ('path ')

Converts a relative path to an absolute path.

19. OS. path. split ('path ')

Split path into two groups: Directory and file name.

ImportOS

Print (OS. path. split ('H:/PythonCheng/Ceshi01/123. py') # ('H:/Python Program/ceshi01 ', '2017. py ')
20. OS. path. dirname (path)

Returns the path directory. It is actually the first element of OS. path. split (path.

ImportOS

Print (OS. getcwd () # H: \ Python program \ ceshi01

Print (OS. path. dirname (OS. getcwd () # H: \ Python Program

21. OS. path. basename ('path ')

The Path is passed into the function as a string. If path ends with/or \, a null value is returned.

ImportOS

Print (OS. getcwd () # H: \ Python program \ ceshi01

A = OS. getcwd ()

Print (OS. path. basename (str (a) # ceshi01

22. OS. path. exists ('path ')

Determine whether a path exists. If yes, TRUE is returned. Otherwise, false is returned.

23. OS. path. isabs ('path ')

Determines whether the path is an absolute path. If yes, TRUE is returned. Otherwise, false is returned.

24. OS. path. isfile ('path ')

If path is an existing file, TRUE is returned; otherwise, false is returned.

25. OS. path. isdir ('path ')

If path is an existing Directory, TRUE is returned; otherwise, false is returned.

26. OS. path. join ('path ')

ImportOS

Print (OS. path. isdir (str (OS. path. join ('H :','/PythonProgram/','Ceshi01') # True

17th, OS. path. getatime ('path ')

Returns the last access timestamp of the file or directory indicated by path.

ImportOS

Print (OS. path. getatime ('H:/PythonProgram/Ceshi01')))

#1509159832.5264933

Twenty-eight, OS. path. getmtime ('path ')

Returns the last modification timestamp of the file or directory pointed to by path.

ImportOS

Print (OS. path. getmtime ('H:/PythonProgram/Ceshi01')))

#1509159832.5264933

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.