Python to create a new folder and copy all the contents of the folder
- 2021-01-06 00:39:32
- OfStack
Create a new folder under the specified path:
import os
def newfile(path):
path=path.strip()
path=path.rstrip("\\")
# Determine if the path exists
isExists=os.path.exists(path)
# There is no
if not isExists:
# Create a directory manipulation function
os.makedirs(path)
print(path+' Creating a successful ')
return True
# There are
else:
print(path+' The directory already exists ')
return False
# Define the directory to create
newpath="F:\\14"
# Call a function
newfile(newpath)
We use two functions from the os module in Python:
os.path.exists: Determine if the path exists
os. makedirs: Generate multiple levels of directory, for example, the path is "F:\18\15", but there is no folder 18 in the root directory of f, you can also create 18, and then create 15 within 18.
Copy all files in 1 folder to the specified path:
copytree function under shutil module is used
import shutil
shutil.copytree('F:/12', 'F:/14')