Skip to content
Travel is a state of mind, an obsession with natural scenery and splendid culture.
Zeyu Xu
  • Home
  • Research
  • Blog
  • Contact
July 4, 2022 by zeyu

Useful file operations in Python

Useful file operations in Python
July 4, 2022 by zeyu

Skills and Methods

1、Determine if the folder exists, and if it does not exist, create.

import os
if not os.path.exists(path):
os.mkdir(path)

2、Traversing folders with files in a specific format.

import os
import fnmatch
img_list = fnmatch.filter(os.listdir(path), '*.png')
for img in img_list :
……

3、Rename the file.

import os
os.rename(Old_Dir,New_Dir)

4、Path connection.

import os
New_Dir=os.path.join(path,filestr,filetype)

5、Copy files.

import shutil
shutil.copy(Old_Dir, New_Dir)

6、Replace the suffix name

jpg_img=files[:-4]+".jpg"

7、Replace characters in path

new_files=files.replace('_','')

Page Views: 116,202

Related

Previous articleThe first MTB Cycling

Blog Navigation

  • Computer skills (3)
  • Life essays (4)
  • Research (1)

Calendar

July 2022
M T W T F S S
 123
45678910
11121314151617
18192021222324
25262728293031
« Jun    
Zeyu