简体   繁体   中英

Making multiple directories in python3

I'm trying to make multiple directories in python but it's saying the file doesn't exist even though I just created it above the code is below, help would be much appreciated.

import os
import shutil

# err
if os.path.isdir(r'\Users\Oran\Documents\Sync\Files\Projects/testProject'):

    shutil.rmtree(r'\Users\Oran\Documents\Sync\Files\Projects/testProject')


# Main
os.chdir(r'\Users\Oran\Documents\Sync\Files\Projects')
os.mkdir('testProject')
os.chdir('testProject')
f = open("index.html", "wt")


dir_names = ["assets", "css"]
for x in (dir_names):
os.mkdir(x)

os.chdir('css/')
f = open("index.css", "wt")

os.chdir('assets/')

dir_names = ["img", "css", "js"]
for x in (dir_names):
os.mkdir(x)

os.chdir('js/')
f = open("index.js", "wt")

Output

[Running] python         
"c:\Users\Oran\Documents\Sync\Files\Projects\PYProjetCreator\index.py"
Traceback (most recent call last):
File 
"c:\Users\Oran\Documents\Sync\Files\Projects\PYProjetCreator\index.py", line 
24, in <module>
os.chdir('assets/')
FileNotFoundError: [WinError 2] The system cannot find the file specified: 
'assets/'

[Done] exited with code=1 in 0.089 seconds

You're trying to change directories into the testProject/css/assets folder instead of the testProject/assets folder.

The quickest way to get there from the css folder is with something like os.chdir('..\\assets') , though there are lots of other ways to maneuver using os.pardir and other os.path manipulations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM