python - 在python3中制作多个目录

标签 python windows python-3.x error-handling directory

我正在尝试在python中创建多个目录,但这是说该文件不存在,即使我只是在下面的代码上方创建了该文件,也非常感谢您的帮助。

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")

输出量
[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

最佳答案

您正在尝试将目录更改为testProject/css/assets文件夹,而不是testProject/assets文件夹。

css文件夹到达那里的最快方法是使用os.chdir('..\assets')之类的东西,尽管还有很多其他方法可以使用os.pardir和其他os.path操作进行操纵。

关于python - 在python3中制作多个目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49435965/

相关文章:

python - 在进程完成之前将日志重定向到文件

python - 如何更正此错误 PyPDF2.utils.PdfReadError : Cannot read an empty file

shell - 如何使用 tox 运行自定义命令而不在 tox.ini 中指定它?

python - Django 简单形式 "object has no attribute ' is_hidden'"

python - 如何使用 matplotlib 将 3d 矩阵映射到 3d 散点图中的颜色值?

windows - 使用 Eclipse 进行跨平台 java 开发(构建路径问题)

c++ - 在 Windows 上的 C++ 中的 for 循环中创建多个目录

windows - 删除 node_modules 文件夹

python-3.x - 下采样非时间序列数据

android - 使用grep 读取文件中pattern1 的日志并仅打印包含pattern1 的行。当在文件中找到pattern2时停止搜索