Usage of os package in python

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

  1. os module: The os module contains general operating system functions in python. Some useful parts in the os module are listed below.

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

Usage of os package in python
The usage of wheel in python
Detailed usage of dictionary in Python
The usage of tuples in python
The usage of Ajax in Python3 crawler
MongoDB usage in Python
The meaning and usage of lists in python
Subscripts of tuples in Python
Summary of logarithm method in Python
Analysis of usage examples of Python yield
Use of Pandas in Python development
Simple usage of python definition class
Python learning os module and usage
Description of in parameterization in python mysql
Analysis of glob in python standard library
Method of installing django module in python
Detailed usage of Python virtual environment venv
Knowledge points of shell execution in python
7 features of Python3.9
What is the function of adb in python
Detailed explanation of python standard library OS module
Python implements the shuffling of the cards in Doudizhu
The usage of several regular expressions in Linux
Detailed explanation of the usage of Python decimal module
The usage of several regular expressions in Linux
The consequences of uninstalling python in ubuntu, very
Example of feature extraction operation implemented in Python
03. Operators in Python entry
Python3 built-in module usage
Join function in Python
12. Network Programming in Python3
print statement in python
Installation and use of GDAL in Python under Ubuntu
Detailed explanation of Python web page parser usage examples
How to understand the introduction of packages in Python
Basics of Python syntax
How to understand a list of numbers in python
Concurrent requests in Python
Python advanced usage summary
Basic syntax of Python
Basic knowledge of Python (1)
Install python in Ubuntu
Example of how to automatically download pictures in python
Context management in Python
Prettytable module of python
Arithmetic operators in python
Write gui in python
09. Common modules of Python3
Str string in Python
Summary of ubuntu usage
Computational Geometry in Python
How to find the area of a circle in python
Learn the hard core operation of Python in one minute
A large inventory of commonly used third-party libraries in Python
Concurrent requests in Python (part 2)
Consolidate the foundation of Python (4)
Python high-order function usage summary!
Consolidate the foundation of Python(7)
In-depth understanding of python list (LIST)
Python analysis of wav files
Consolidate the foundation of Python(6)