Python Learning Diary os and sys modules

os module

1.os.getcwd()

Gets the current working directory, which is the path to the directory where the current python script works

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

2.os.chdir()

Change the current script working directory; equivalent to cd under 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()

Used to create directories recursively with two parameters, path, mode (permission mode)

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

Before code execution:

After code execution:

4.os.removedirs()

Used to delete directories recursively, delete directories if they are empty, return to the previous directory, delete directories if they are empty, and so on; if directories are not empty, there is no way to delete them, and the program will error

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

Before code execution:

After code execution:

5.os.mkdir()

The default permission numeric mode is 0777 (octal)

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

Before code execution:

After code execution:

 

6.os.listdir()

Returns a list of files or folder names contained in a specified folder. This list is alphabetical and does not include'. 'and'..' even though it is in a folder and only supported under Unix,Windows

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()

Used to make a system stat call on a given path

Parameters:

st_mode: inode protection mode
st_ino: inode node number.
St_dev: The device where the inode resides.
St_nlink: Number of links to inode.
st_uid: User ID of the owner.
st_gid: The group ID of the owner.
st_size: The size of a regular file in bytes; contains data waiting for certain special files.
st_atime: Last visited time.
st_mtime: Last modified time.
st_ctime: The "ctime" reported by the operating system.On some systems (such as Unix) the time of the most recent metadata change, and on others (such as Windows) the time of creation (see the platform's documentation for details).
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 command to display directly

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()

Run shell command to get execution results

import os
print(os.popen('dir').read())
#  Driver C Volumes in are systems
#  Volume serial number is 85 C0-669A
# 
#  C:\Users\Administrator\PycharmProjects\PYL\temp_file Directory
# 
# 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 Files             52 bytes
#                4 45 directories,363,744,768 Available Bytes

10.os.rename()

Rename File/Directory

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

Before program execution:

After program execution:

11.os.path.getsize()

Returns the content size of all folders in this path

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

12.os.path.abspath() 

Returns path normalized absolute path

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

13.os.path.split()

Split path into directory and file name binaries Return

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

14.os.path.dirname()

Returns the path's directory, which is actually the first element of 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()

Returns the last file name of the path, and if the path ends with/or\, it returns a null value, the second element of 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()

Returns True if path exists; False if path does not exist

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()

Return True if path is absolute

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()

If path is an existing file, return True, otherwise return 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()

Returns True if path is an existing directory, False otherwise

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()

When multiple paths are combined, the parameters before the first absolute path are ignored

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()

Returns the last access time of the file or directory pointed to by 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('Last File Access Time:' + time.asctime(time.localtime(times)))      #Last File Access Time:Sat Aug 31 22:30:23 2019

22.os.path.getmtime()

Returns the last modified time of the file or directory pointed to by 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('Last Modified File Time:' + time.asctime(time.localtime(times)))      #Last File Access Time:Sat Aug 31 22:30:23 2019

23.os Module Properties

os.sep outputs operating system-specific path delimiters, with'\'under win and'/' under Linux
os.linesep outputs the line terminator used by the current platform, with'\r\n'under win and'\n' under Linux
The os.pathsep output is under the string win used to split the file path; under Linux is:
The os.name output string indicates that the platform is currently in use.Win->'nt'; Linux->'posix'

 

sys module

1.sys.platform

Return operating system platform name

import sys
print(sys.platform)     #win32

2.sys.argv

Command Line Parameter List, the first element is the program path itself

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

3.sys.exit(n)

Exit the program, exit(0) on normal exit, exit sys.exit(1) on error

4.sys.version

Get version information for python interpreter

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

5.sys.path

Returns the module's search path, initializing with the value of the PYTHONPATH environment variable

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']

Tags: Python Linux shell Anaconda

Posted on Sat, 31 Aug 2019 09:52:57 -0700 by dross613