1 Create a directory and determine whether it exists, if it does not exist, create
import os
# Directory created
path ="yyy"if not os.path.exists(path):
os.makedirs(path)
os.path.exists('d:/assist/getTeacherList.py') #True or False
2 Subfolders and files in the output folder
import os
filePath ='E:\BaiduNetdiskDownload\data\Manual hoeing'for file inrange(len(os.listdir(filePath))):
filepath_in = filePath +'/'+str(os.listdir(filePath)[file])
# print(filepath_in)
k =0for data_file inrange(len(os.listdir(filepath_in))):
filepath_data = filepath_in +'/'+str(os.listdir(filepath_in)[data_file])
k +=1print(k," ",filepath_data)
3 Folders and files in the output folder (the second type)
import os
filePath ='E:\BaiduNetdiskDownload\data\Manual hoeing/'for i,j,k in os.walk(filePath):print(i,j,k)
Appendix: Let’s take a look at the common methods of os in python
os.sep can replace the operating system specific path separator. "\" under windows
The os.name string indicates the platform you are using. For example, for Windows, it is'nt', and for Linux/Unix users, it is'posix'.
The os.getcwd() function gets the current working directory, which is the directory path where the current Python script works.
os.getenv() gets an environment variable, if it does not return none
os.putenv(key, value) sets an environment variable value
os.listdir(path) returns the names of all files and directories in the specified directory.
The os.remove(path) function is used to delete a file.
The os.system(command) function is used to run shell commands.
The os.linesep string gives the line terminator used by the current platform. For example, Windows uses'\r\n', Linux uses'\n' and Mac uses'\r'.
os.curdir: returns the current directory ('.')
os.chdir(dirname): change the working directory to dirname
========================================================================================
Common methods of os.path:
os.getcwd() Get the current working directory, that is, the directory path where the current python script works
os.chdir("dirname") changes the working directory of the current script; equivalent to cd in the shell
os.curdir returns the current directory: ('.')
os.pardir gets the parent directory string name of the current directory: ('..')
os.makedirs('dirname1/dirname2') can generate multi-layer recursive directories
os.removedirs('dirname1') If the directory is empty, delete it and recursively to the upper-level directory, if it is also empty, delete it, and so on
os.mkdir('dirname') generates a single-level directory; equivalent to mkdir dirname in the shell
os.rmdir('dirname') Delete a single-level empty directory. If the directory is not empty, it cannot be deleted and an error is reported; equivalent to rmdir dirname in the shell
os.listdir('dirname') List all files and subdirectories in the specified directory, including hidden files, and print them in a list
os.remove() delete a file
os.rename("oldname","newname") Rename file/directory
os.stat('path/filename') Get file/directory information
os.sep output operating system specific path separator, "\" under win, "/" under Linux
os.linesep outputs the line terminator used by the current platform, "\t\n" under win, "\n" under Linux
os.pathsep outputs the string used to split the file path under win; under Linux, it is:
The os.name output string indicates the current platform. win-'nt'; Linux-'posix'
os.system("bash command") Run shell commands and display directly
os.environ Get system environment variables
os.path.abspath(path) returns the normalized absolute path of path
os.path.split(path) Split path into a two-tuple of directory and file name and return
os.path.dirname(path) returns the directory of path. Is actually the first element of os.path.split(path)
os.path.basename(path) returns the last file name of path. If the path ends with / or , then a null value will be returned. That is, the second element of os.path.split(path)
os.path.exists(path) If path exists, return True; if path does not exist, return False
os.path.isabs(path) If path is an absolute path, return True
os.path.isfile(path) If path is an existing file, return True. Otherwise return False
os.path.isdir(path) If path is an existing directory, return True. Otherwise return False
os.path.join(path1[, path2[, …]]) Combine multiple paths and return, the parameters before the first absolute path will be ignored
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
os.path.getsize(path) returns the size of path
os.path.normpath(os.path.join(os.path.abspath(file),'..','..','..')) means to return the upper and upper directory of the current file
to sum up
So far, this article on the usage of the os package in python is introduced. For more related python os package usage, please search ZaLou.Cn
Recommended Posts