In this chapter, we’ll come across another common routine, before that, you’ll have to go look for the basic python knowledge, because we’re not gonna talk about them again.
OS
Note that folder names and file names are not case-sensitive on Windows and OS X, they are only case-sensitive on Linux.
And on Windows, we use backslashes \ as separators between folders to organize path. But on OS X and Linux, we use forward slashes / as their path separators.
1
import os, sys
1 2 3 4 5
os.path.join('Program Files', 'Listary', 'Images') ## Windows #'Program Files\\Listary\\Images' ## OS X and Linux #'Program Files/Listary/Images'
os.getcwd() : Get Current working directory,
os.chdir(): Change working directory,
1 2 3 4 5
os.getcwd() # 'H:\\cocofiles\\Python' os.chdir('H:/dorofiles/jupyter') ## return FileNotFoundError if not exists os.getcwd() # 'H:\\dorofiles\\jupyter'
os.listdir("H:\oceanfiles\V2Ray\doc") # ['readme.md'] os.mkdir('Tool') ## return error if dir already exists os.mkdir('Tool\hello') ## recursive , return error if the 1st dir already exists os.makedirs('Tool\hello', exist_ok=True)
path = "/tmp/home/monthly/daily" os.makedirs( path, 0o777 ) ## create recursive folders in root, mode is 777
Opens the file in binary (untranslated) mode. (See fopen for a description of binary mode.)
_O_CREAT
Creates a file and opens it for writing. Has no effect if the file specified by filename exists. The pmode argument is required when _O_CREAT is specified.
_O_APPEND
Moves the file pointer to the end of the file before every write operation.
_O_CREAT | _O_TEMPORARY
Creates a file as temporary; the file is deleted when the last file descriptor is closed. The pmode argument is required when _O_CREAT is specified.
_O_CREAT | _O_EXCL
Returns an error value if a file specified by filename exists. Applies only when used with _O_CREAT.
_O_NOINHERIT
Prevents creation of a shared file descriptor.
_O_RANDOM
Specifies that caching is optimized for, but not restricted to, random access from disk.
_O_RDONLY
Opens a file for reading only. Cannot be specified with _O_RDWR or _O_WRONLY.
_O_RDWR
Opens a file for both reading and writing. Cannot be specified with _O_RDONLY or _O_WRONLY.
_O_SEQUENTIAL
Specifies that caching is optimized for, but not restricted to, sequential access from disk.
Opens a file and truncates it to zero length; the file must have write permission. Cannot be specified with _O_RDONLY. _O_TRUNC used with _O_CREAT opens an existing file or creates a file. Note: The _O_TRUNC flag destroys the contents of the specified file.
_O_WRONLY
Opens a file for writing only. Cannot be specified with _O_RDONLY or _O_RDWR.
path = 'C:\\Windows\\System32\\calc.exe' path.split(os.path.sep) ## Windows # ['C:', 'Windows', 'System32', 'calc.exe'] '/usr/bin'.split(os.path.sep) ## OS X and Linux # ['', 'usr', 'bin']
import os fileDict = dict() path = 'C:/Users/Administrator/.ssh/' for f in os.listdir(path): ## Calculate attributes for all files listed here. fileStats = os.stat(path + f) fileDict[f] = fileStats print(fileStats) for item in fileDict: print("The File: {:30s} The Size: {:d} Bytes".format(item, fileDict[item].st_size))
1 2 3 4 5 6
os.stat_result(st_mode=33206, st_ino=5348024557680855, st_dev=1651831854, st_nlink=1, st_uid=0, st_gid=0, st_size=1679, st_atime=1577672408, st_mtime=1577672408, st_ctime=1577672408) os.stat_result(st_mode=33206, st_ino=13510798882289897, st_dev=1651831854, st_nlink=1, st_uid=0, st_gid=0, st_size=400, st_atime=1577672408, st_mtime=1577672408, st_ctime=1577672408) os.stat_result(st_mode=33206, st_ino=27021597764384213, st_dev=1651831854, st_nlink=1, st_uid=0, st_gid=0, st_size=1382, st_atime=1577672622, st_mtime=1584688410, st_ctime=1577672622) The File: id_rsa The Size: 1679 Bytes The File: id_rsa.pub The Size: 400 Bytes The File: known_hosts The Size: 1382 Bytes
defgetFile(filePath, pat="py", files=[]): fl = os.listdir(filePath) ## list all files in path try: for f in fl: tmpath = os.path.join(filePath, f) #== path + '/' + f if os.path.isdir(tmpath): ## check if it is a dir getFile(tmpath, filePath, files) ## recursive elif tmpath[tmpath.rfind('.') + 1:].lower() == pat: files.append(tmpath) ## find that filename extension and add to the tmppath else: return files except PermissionError: print("Permission denied: '%s'" % filePath) whileTrue: path = input('Input Your Path Here:').strip() if os.path.isdir(path) == True: print("Path:", path) break # path = 'D:/networkTools/Fiddler' txtfiles = getFile(path, "txt") print(txtfiles)
Create a file named HIDEME.txt(readonly and automatically hide)
1 2 3 4 5 6 7 8
os.access("HIDEME.txt", os.F_OK) ## if exits # True os.access("HIDEME.txt", os.R_OK) ## if readable # True os.access("HIDEME.txt", os.W_OK) ## if writable # True os.access("HIDEME.txt", os.X_OK) ## if excutable # False
os.system
os.system() method execute the command (a string) in a subshell.
1 2 3 4 5
import os ## importing os module # dir(os) os.system('mkdir py') ## Command to execute and make a py directory os.system('notepad') ## open notepad.exe os.system('notepad python.txt')
os.walk
os.listdir() only list files in current folder, but os.walk will walk through all files and subfolders in current folder.
import os for folderName, subfolders, filenames in os.walk('C:\\Users\\Administrator\\.jupyter'): print('The current folder is ' + folderName) for subfolder in subfolders: print('SUBFOLDER OF ' + folderName + ': ' + subfolder) for filename in filenames: print('FILE INSIDE ' + folderName + ': '+ filename) print('')
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
The current folder is C:\Users\Administrator\.jupyter SUBFOLDER OF C:\Users\Administrator\.jupyter: lab FILE INSIDE C:\Users\Administrator\.jupyter: jupyter_notebook_config.py FILE INSIDE C:\Users\Administrator\.jupyter: migrated
The current folder is C:\Users\Administrator\.jupyter\lab SUBFOLDER OF C:\Users\Administrator\.jupyter\lab: user-settings SUBFOLDER OF C:\Users\Administrator\.jupyter\lab: workspaces
--snip--
The current folder is C:\Users\Administrator\.jupyter\lab\workspaces FILE INSIDE C:\Users\Administrator\.jupyter\lab\workspaces: lab-a511.jupyterlab-workspace FILE INSIDE C:\Users\Administrator\.jupyter\lab\workspaces: labworkspacesauto-2-f9e3.jupyterlab-workspace FILE INSIDE C:\Users\Administrator\.jupyter\lab\workspaces: labworkspacesauto-i-72c8.jupyterlab-workspace
Sys
1 2 3 4 5 6 7
import sys f = [x ** 2for x in range(1, 100000)] print(sys.getsizeof(f)) # 824464 f = (x ** 2for x in range(1, 100000)) print(sys.getsizeof(f)) # 120
picFormats = ['jpg', 'png', 'gif', 'jpeg', 'bmp', 'webp'] print('Plz enter a file name:') file = input() if file.lower().split(".")[-1] notin picFormats: print('Maybe not an image file. ') else: print(file + ' is an image file.')
$ python py_sys_argv_test.py # ['py_sys_argv_test.py'] # Hello, world! $ python py_sys_argv_test.py Max # ['py_sys_argv_test.py', 'Max'] # Hello, Max! $ python py_sys_argv_test.py Max Chloe # ['py_sys_argv_test.py', 'Max', 'Chloe'] # Too many arguments!
py_sys_argv_lines.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
import sys for arg in sys.argv: try: # ignore UnicodeDecodeError: 'gbk' codec can't decode byte 0xac in position f = open(arg, 'r', errors='ignore') s = f.readline() except OSError as err: print("OS error: {0}".format(err)) except IOError: print('cannot open', arg) except: print("Unexpected error:", sys.exc_info()[0]) raise else: print(arg, 'has', len(f.readlines())+1, 'lines') f.close()
1 2 3 4 5 6 7 8 9 10
$ python py_sys_argv_lines.py thoughts.txt py_sys_lines.py has 15 lines thoughts.txt has 1 lines
$ python py_sys_argv_lines.py thoughcts.txt py_sys_lines.py has 15 lines OS error: [Errno 2] No such file or directory: 'thoughcts.txt' $ python py_sys_argv_lines.py 红楼梦.txt py_sys_lines.py has 15 lines 红楼梦.txt has 5541 lines
我们将for arg in sys.argv: 改为 for arg in sys.argv[1:]:,防止统计脚本本身的行数:
1 2 3
$ python py_sys_argv_lines.py 红楼梦.txt Tagore.txt 红楼梦.txt has 5541 lines Tagore.txt has 8 lines