Python os operation file

Rookie tutorial os module explained

1. Import os module when using

import os;

1.1 Print a function directly to see

print(os.getcwd());

The result is the current relative path:

2. About path slash (/) and backslash (/)

The backslash (\) is used as the separator between folders in Windows. The
slash (/) is used as the separator between folders in Mac and Linux. The
backslash is used to escape in Python,
so it becomes two. Backslashes \

After connecting the file, windows is a backslash

import os;

print(os.path.join('Pywww','helloworld'));

3. Absolute path and relative path

Absolute path: start from the root folder
Windows: start from the C drive, D drive, etc.
For example: C \ Users \ Anderson \ Documents
Mac and Linux: it is /
for example: / Users / Anderson

Relative path: relative to the current running directory of the program. For
example, our program runs in the E: \ PythonPractice \
folder. There is also a folder called practice1 in
this folder. The relative path of the practice1 folder is ./practice1

List the current directory and the folders under the directory:

import os;

print(os.listdir());

Use the for loop to print the current directory and the folders under the directory:

import os;

for item in os.listdir():
    print(item)

Guess you like

Origin www.cnblogs.com/yaoliuyang/p/12755485.html