preface
OS module although the basic time has learned, but who let me belong to the kind of people who do not immediately forget it, so in the next climb to a nameless man all like the site, in the encounter climb down the data need to save, you need to use the OS module
Review of OS module basics
A review of the basic OS module used to manipulate folders (based on my understanding)
import os
os.mkdir("path") Create the path
Copy the code
And then, the foundation is gone. Yeah, I learned this when I learned the basic OS module, which was enough at the time
OS module applet
This is the following I write crawler small program when used
1. An error occurs when the file is saved because the folder already exists
import os
path = "File path"
if not os.path.exists(path):
os.mkdir(path)
Copy the code
If path does not exist, create it; if path does exist, do not create it
2. View the current file path
import os
print(os.getcwd())
Copy the code
This is still very useful, before using mobile phone to write crawler, is to use this to see the current path, to save the data, as for climbing is what data, this, of course, is the kind of men love to see O(∩_∩)O ha ha ~
3. Copy the file and rename the copy
import os
# The first way
os.rename("File name to copy"."Rename copied file")
# Second way
os.system(Copy Name of the file to be copied Rename the file after copying)
# open CMD rename, which is equivalent to opening CMD for you, and typing this command, note that the filename must be an absolute path
Copy the code
conclusion
That’s all THE OS I’m going to use for now, maybe it’s not good enough