The python crawler automatically creates folders

  • 2020-12-05 17:18:41
  • OfStack

The crawler applies the folder creation function:


#file setting
folder_path = "D:/spider_things/2016.4.6/" + file_name +"/"
if not os.path.exists(folder_path):
os.makedirs(folder_path)

The code block above means:

"os.path.exists(folder_path)" Used to determine folder_path Does this path exist? If not, execute" os.makedirs(folder_path)” To create this path

Supplement: Let's take a look at the Python crawler - file and folder operations

0. File name, path information, extension, etc


# File suffix 
>>> os.path.splitext("/root/a.py")
('/root/a', '.py')
# Get the directory and filename 
>>> os.path.split("/root/a.py")
('/root', 'a.py')
>>> os.path.basename("/root/a.py")
'a.py'
>>> os.path.dirname('/root/a.py')
'/root'

1. Walk through folders and rename


import os
import sys
path = 'D:/emojis'
# os.walk  Returns the 1 a 3 tuples 
for (path, dirs, files) in os.walk(path):
  for filename in files:
    newname = "emoji_" +filename
    os.rename(os.path.join(path, filename) , os.path.join(path, new_name))

conclusion


Related articles: