Python learning diary (xvii) os module and sys module

os module

1.os.getcwd()

Gets the current working directory that the current directory path python script work

import os
print(os.getcwd())       #C:\Users\Administrator\PycharmProjects\PYL\temp_file

2.os.chdir()

Script to change the current working directory; cd at the equivalent of shell

import os
print(os.getcwd())       #C:\Users\Administrator\PycharmProjects\PYL\temp_file
os.chdir(r'C:\Users\Administrator\PycharmProjects\PYL')
print(os.getcwd())       #C:\Users\Administrator\PycharmProjects\PYL

3.os.makedirs ()

Recursively create directories, there are two internal parameters path, mode (permissions mode)

import os
os.makedirs('dirname1/dirname2/dirname3')

Before the code is executed:

Code execution:

4.os.removedirs()

Recursively delete a directory, if the directory is empty deleted, and return to the parent directory, if also empty, delete, and so on; if the directory is not empty there is no way to delete the program reports an error

import os
os.removedirs('dirname1/dirname2/dirname3')

Before the code is executed:

Code execution:

5.os.mkdir()

And makedirs () to create a level can only be compared to the directory, the default permissions digital mode is 0777 (octal)

import os
os.mkdir('dirname1/dirname2')

Before the code is executed:

Code execution:

 

6.os.listdir ()

List the name of the file or return to the specified folder contains folders. This list is in alphabetical order, it does not include '' and '..' even if it is in a folder, only supported under Unix, Windows use

import os
path = os.getcwd()
print(path)                                                                         #C:\Users\Administrator\PycharmProjects\PYL\temp_file
print(os.listdir(path))                                                             #['dirname1', 'temp_py.py']
print(os.listdir(r'C:\Users\Administrator\PycharmProjects\PYL\temp_file'))         #['dirname1', 'temp_py.py']

7.os.stat ()

A stat system call execution on a given path

parameter:

st_mode: inode protection mode 
st_ino: inode node number. 
st_dev: inode device resides. 
st_nlink: inode number of links. 
st_uid: User ID of the owner. 
st_gid: group ID of the owner. 
st_size: common file unit size in bytes; wait for data containing special file. 
st_atime: Time last accessed. 
st_mtime: last modification time. 
st_ctime: report by the operating system's " ctime " . On some systems (such as Unix) is the time of most recent metadata changes, on the other systems (such as Windows) is the creation time (see platform documentation for more information).
import os
path = os.getcwd()
print(path)             #C:\Users\Administrator\PycharmProjects\PYL\temp_file
get_path = os.stat(path)
print(get_path)         #os.stat_result(st_mode=16895, st_ino=51791395714972605, st_dev=2243978906, st_nlink=1, st_uid=0, st_gid=0, st_size=4096, st_atime=1567246056, st_mtime=1567246056, st_ctime=1567058187)
get_path2 = os.stat('test.py')
print(get_path2)        #os.stat_result(st_mode=33206, st_ino=38280596832774340, st_dev=2243978906, st_nlink=1, st_uid=0, st_gid=0, st_size=12, st_atime=1567245950, st_mtime=1567245950, st_ctime=1567245924)

8.os.system ()

Run shell commands, direct display

import os
print(os.system('dir'))
#  ������ C �еľ��� ϵͳ
#  �������� 85C0-669A
# 
#  C:\Users\Administrator\PycharmProjects\PYL\temp_file ��Ŀ¼
# 
# 2019/08/31  22:00    <DIR>          .
# 2019/08/31  22:00    <DIR>          ..
# 2019/08/31  17:40    <DIR>          dirname1
# 2019/08/31  22:00                34 temp_py.py
# 2019/08/31  18:05                12 test.py
#                2 ���ļ�             46 �ֽ�
#                3 ��Ŀ¼ 45,381,369,856 �����ֽ�
# 0

9.os.popen()

运行shell命令,获取执行结果

import os
print(os.popen('dir').read())
#  驱动器 C 中的卷是 系统
#  卷的序列号是 85C0-669A
# 
#  C:\Users\Administrator\PycharmProjects\PYL\temp_file 的目录
# 
# 2019/08/31  22:08    <DIR>          .
# 2019/08/31  22:08    <DIR>          ..
# 2019/08/31  17:40    <DIR>          dirname1
# 2019/08/31  22:04    <DIR>          nwdir
# 2019/08/31  22:08                40 temp_py.py
# 2019/08/31  18:05                12 test.py
#                2 个文件             52 字节
#                4 个目录 45,363,744,768 可用字节

10.os.rename()

重命名文件/目录

import os
os.rename('old_test.py','new_test.py')

程序执行前:

程序执行后:

11.os.path.getsize()

返回这个路径下的所有文件夹的内容大小

import os
path = os.getcwd()
path_size = os.path.getsize(path)
print(path_size)              #4096

12.os.path.abspath() 

返回path规范化的绝对路径

import os
path = os.getcwd()
print(os.path.abspath(path))        #C:\Users\Administrator\PycharmProjects\PYL\temp_file

13.os.path.split()

将path分割成目录和文件名二元组返回 

import os
path = os.getcwd()
print(os.path.split(path))        #('C:\\Users\\Administrator\\PycharmProjects\\PYL', 'temp_file')

14.os.path.dirname()

返回path的目录,其实就是os.path.split(path)的第一个元素 

import os
path = os.getcwd()
print(path)                         #C:\Users\Administrator\PycharmProjects\PYL\temp_file
print(os.path.dirname(path))        #C:\Users\Administrator\PycharmProjects\PYL

15.os.path.basename()

返回path最后的文件名,如果path以/或\结尾,那么就会返回空值,即os.path.split(path)的第二个元素

import os
path = os.getcwd()
print(path)                         #C:\Users\Administrator\PycharmProjects\PYL\temp_file
print(os.path.basename(path))       #temp_file

16.os.path.exists()

如果path存在,返回True;如果path不存在,返回False

import os
path = os.getcwd()
print(path)                                                                             #C:\Users\Administrator\PycharmProjects\PYL\temp_file
print(os.path.exists(path))                                                             #True
print(os.path.exists(r'C:\Users\Administrator\PycharmProjects\PYL\temp_file2'))         #False

17.os.path.isabs()

如果path是绝对路径,返回True

import os
path = os.getcwd()
print(path)                                                                             #C:\Users\Administrator\PycharmProjects\PYL\temp_file
print(os.path.isabs(path))                                                              #True
print(os.path.isabs(r'.\temp_file'))                                                    #False

18.os.path.isfile()

如果path是一个存在的文件,返回True,否则返回False

import os
path = os.getcwd()
print(path)                                                                                                     #C:\Users\Administrator\PycharmProjects\PYL\temp_file
print(os.path.isfile(path))                                                                                     #False
print(os.path.isfile(r'C:\Users\Administrator\PycharmProjects\PYL\temp_file\dirname1\dirname2\temp_py.py'))     #True

19.os.path.isdir()

如果path是一个存在的目录,则返回True,否则返回False

import os
path = os.getcwd()
print(path)                                                                                                     #C:\Users\Administrator\PycharmProjects\PYL\temp_file
print(os.path.isdir(path))                                                                                      #True
print(os.path.isdir(r'C:\Users\Administrator\PycharmProjects\PYL\temp_file\dirname1\dirname2\temp_py.py'))      #False

20.os.path.isjoin()

将多个路径组合后返回,第一个绝对路径之前的参数将被忽略

import os
path = os.getcwd()
print(path)                                                               
#C:\Users\Administrator\PycharmProjects\PYL\temp_file
print(os.path.join(path,r'C:\Users\Administrator\PycharmProjects\PYL\temp_file\new_test.py'))
# C:\Users\Administrator\PycharmProjects\PYL\temp_file
# C:\Users\Administrator\PycharmProjects\PYL\temp_file\new_test.py

21.os.path.getatime()

返回path所指向的文件或者目录的最后访问时间

import os,time
path = os.getcwd()
print(path)                                                         #C:\Users\Administrator\PycharmProjects\PYL\temp_file
times = os.path.getatime(path)
print(times)                                                        #1567261823.2424655
print('文件最后访问时间:' + time.asctime(time.localtime(times)))      #文件最后访问时间:Sat Aug 31 22:30:23 2019

22.os.path.getmtime()

返回path所指向的文件或者目录的最后修改时间

import os,time
path = os.getcwd()
print(path)                                                         #C:\Users\Administrator\PycharmProjects\PYL\temp_file
times = os.path.getmtime(path)
print(times)                                                        #1567261823.2424655
print('文件最后修改时间:' + time.asctime(time.localtime(times)))      #文件最后访问时间:Sat Aug 31 22:30:23 2019

23.os模块的属性

os.sep     输出操作系统特定的路径分隔符,win下为"\\",Linux下为"/"
os.linesep    输出当前平台使用的行终止符,win下为"\r\n",Linux下为"\n"
os.pathsep    输出用于分割文件路径的字符串 win下为;,Linux下为:
os.name    输出字符串指示当前使用平台。win->'nt'; Linux->'posix'

 

sys模块

1.sys.platform

返回操作系统平台名称

import sys
print(sys.platform)     #win32

2.sys.argv

命令行参数List,第一个元素是程序本身路径

import sys
print(sys.argv)      #['C:\\Users\\Administrator\\PycharmProjects\\PYL\\temp_file\\dirname1\\dirname2\\temp_py.py']

3.sys.exit(n)

退出程序,正常退出时exit(0),错误退出sys.exit(1)

4.sys.version

获取python解释程序的版本信息

import sys
print(sys.version)      #3.7.1 (default, Dec 10 2018, 22:54:23) [MSC v.1915 64 bit (AMD64)]

5.sys.path

返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值

import sys
print(sys.path)      #['C:\\Users\\Administrator\\PycharmProjects\\PYL\\temp_file\\dirname1\\dirname2', 
                     # 'C:\\Users\\Administrator\\PycharmProjects\\PYL', 
                     # 'C:\\Users\\Administrator\\PycharmProjects\\PYL\\venv\\Scripts\\python37.zip', 
                     # 'F:\\Anaconda\\DLLs', 
                     # 'F:\\Anaconda\\lib', 
                     # 'F:\\Anaconda', 
                     # 'C:\\Users\\Administrator\\PycharmProjects\\PYL\\venv', 
                     # 'C:\\Users\\Administrator\\PycharmProjects\\PYL\\venv\\lib\\site-packages', 
                     # 'C:\\Users\\Administrator\\PycharmProjects\\PYL\\venv\\lib\\site-packages\\setuptools-40.8.0-py3.7.egg', 
                     # 'C:\\Users\\Administrator\\PycharmProjects\\PYL\\venv\\lib\\site-packages\\pip-19.0.3-py3.7.egg']

Guess you like

Origin www.cnblogs.com/Fantac/p/11441096.html