Common file and directory operations in python (2)

OS module

I. Overview

The python os module provides a very rich set of methods for working with files and directories

2. Import: import os

3. Common methods

1. The os.name output string indicates the platform being used, if it is windows, it is represented by "nt", if it is Linux/Unix, it is "posix"

import them

print(os.name)

result:
nt

2. os.getcwd() Get the current working directory, that is, the directory path where the current python script works

import them

print(os.getcwd())

result:
D:\python_workshop\python6\study

3. os.listdir(path) returns all files and directory names in the specified directory in the form of a list

import them

dirs = os.listdir(r"D:\python_workshop\python6")
print(dirs)

for file in dirs:
    print(file)

result: [
'.idea', 'alice.txt', 'homeworks', 'lessons', 'personal_practices', 'study', 'url.txt'] .idea alice.txt homeworks lessons personal_practices study url.txt

4. os.mkdir(path, [mode]) Create a directory, the default mode is 0777

import them

path = "./test"
os.mkdir(path, 0o755)

print ( " Directory created " )

result: directory created

5. os.makedirs(path) creates a series of directories, recursively

import them

path = "./test/android_app/auto_test"
os.makedirs(path, 0o755)

print ( " Recursive directory created " )

result: recursive directory created

6. os.remove() removes a file

import them

path = " ../ " 
print ( " The directory file before removal is: " , os.listdir(path))

os.remove( " ../math.txt " )
 print ( "The removed directory file is: " , os.listdir(path))

result: The catalog file before removal is: [
' .idea ' , ' alice.txt ' , ' homeworks ' , ' lessons ' , ' math.txt ' , ' personal_practices ' , ' study ' , ' url.txt ' ] The removed catalog files are: [ ' .idea ' , ' alice.txt ' , ' homeworks ' , ' lessons ' , ' personal_practices ' , ' study ' , ' url.txt ' ]

7. os.rmdir() deletes an empty directory. If the directory is not empty, an OSError will be reported

import them

path = " ../ " 
print ( " The directory file before deletion is: " , os.listdir(path))

os.rmdir( " ../test " )
 print ( "The deleted directory file is: " , os.listdir(path))


result:
The catalog file before deletion is: [ ' .idea ' , ' alice.txt ' , ' homeworks ' , ' lessons ' , ' personal_practices ' , ' study ' , ' test ' , ' url.txt ' ]
The deleted catalog file is: [ ' .idea ' , ' alice.txt ' , ' homeworks ' , ' lessons ' , ' personal_practices ' , ' study ' , ' url.txt ' ]

8. os.environ.get("path") to get the value of the environment variable

 

import them

print(os.environ.get("path"))


result:
C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\OpenCL SDK\3.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\3.0\bin\x64;D:\Program\python34;D:\Program\python34\Scripts;D:\Program\python34\Lib;D:\Program\python34\PyInstaller-3.3.1;D:\Program\python34\lib\site-packages\pywin32_system32

9. os.system(command) run system command

 

import them

os.system('echo "hello world!"')


result:
"hello world!"

 

Four. os module path method

1. os.path.join(path, name) joins a directory with a file name or directory

import them

path = " D:\\python_workshop " 
name = " hello_world "                   # can be a directory or file name that does not exist

print(os.path.join(path, name))


result:
D:\python_workshop\hello_world

2. The os.path.isfile(path) and os.path.isdir(path) functions respectively check whether the given path is a file or a directory, and returns True if it exists, otherwise False

import them

path = "./url.txt"
print(os.path.isfile(path))

path = "../python6"
print(os.path.isdir(path))


result:
False
False

3. os.path.exists(path) is used to check whether the given path really exists, it returns True if it exists, otherwise it is False

import them

path = "../test"
print(os.path.exists(path))


result:
True

4. os.path.split(path) returns the directory name and file name of a path (can be a directory or a file, if the last one is a directory, it is the directory name)

import them

path = r"D:\python_workshop"
print(os.path.split(path))


result:
('D:\\', 'python_workshop')

5. os.path.splitext(path) separates the file name and extension (the path does not really exist)

import them

path = r"D:\python_workshop\python6\lesson1.py"
print(os.path.splitext(path))


result:
('D:\\python_workshop\\python6\\lesson1', '.py')

6. os.path.dirname(path) returns the file path (note that it is not the full path of the file)

import them

path = r"D:\python_workshop\python6"
print(os.path.dirname(path))


result:
D:\python_workshop

7. os.path.basename(path) returns the file path

import them

path = r"D:\python_workshop\python6"
print(os.path.basename(path))


result:
python6

8. os.path.getsize(name) Get the file size, if name is a directory, return OL

import them

name = r"D:\python_workshop\1.txt"
print(os.path.getsize(name))

name = r"D:\python_workshop"
print(os.path.getsize(name))


result:
197
4096

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325112456&siteId=291194637