繁体   English   中英

无法创建文件夹 - FileNotFoundError: [WinError 2] 系统找不到文件

[英]Can't create folders - FileNotFoundError: [WinError 2] The system cannot find the file

我正在尝试做有史以来最简单的事情,但我无法让它发挥作用。

我在我的工作目录中,我们称之为“WorkDir”,就是这样: C:\\WorkDir

我想创建:

newpath = 'C:\WorkDir\Video\Files'

if not os.path.exists(newpath):
    os.makedirs(newpath)

FileNotFoundError: [WinError 2] The system cannot find the file specified: 'C:\\WorkDir\\Video'

我不明白这个错误。 当然它找不到文件,它不存在。 显然我做错了什么,但无法弄清楚。

我也尝试使用句号 '.' 表示工作目录,但这也不起作用。

这些都不起作用:

# raw string 'r'
newpath = r'C:\WorkDir\Video\Files'

if not os.path.exists(newpath):
    os.makedirs(newpath)
# forward slashes
newpath = 'C:/WorkDir/Video/Files'

if not os.path.exists(newpath):
    os.makedirs(newpath)
# period
newpath = '.\WorkDir\Video\Files'

if not os.path.exists(newpath):
    os.makedirs(newpath)
# raw string
newpath = r'.\WorkDir\Video\Files'

if not os.path.exists(newpath):
    os.makedirs(newpath)


FileNotFoundError: [WinError 2] The system cannot find the file specified: '.\\WorkDir'

据我所知,我正在逐字复制stackoverflow帖子。 想不通

奇怪的是我可以直接在 C: 盘创建一个新目录,比如:

# create new folder RandomFolder
newpath = r'C:\RandomFolder\Video\Files'

if not os.path.exists(newpath):
    os.makedirs(newpath)

但是,如果我尝试在工作目录中执行任何操作,则会出现错误。

编辑:完整错误:

FileNotFoundError                         Traceback (most recent call last)
<ipython-input-10-c7d3eec16936> in <module>
      2 
      3 if not os.path.exists(newpath):
----> 4     os.makedirs(newpath)
      5 
      6 # could add number of records to file name too

~\Anaconda3\lib\os.py in makedirs(name, mode, exist_ok)
    209     if head and tail and not path.exists(head):
    210         try:
--> 211             makedirs(head, exist_ok=exist_ok)
    212         except FileExistsError:
    213             # Defeats race condition when another thread created the path

~\Anaconda3\lib\os.py in makedirs(name, mode, exist_ok)
    219             return
    220     try:
--> 221         mkdir(name, mode)
    222     except OSError:
    223         # Cannot rely on checking for EEXIST, since the operating system

FileNotFoundError: [WinError 2] The system cannot find the file specified: 'C:\\WorkDir\\Video'

编辑 2,发现一些东西:所以我刚刚注意到在我的笔记本顶部,自动保存失败。 可能跟这个有关系。 让我调查一下。 抱歉误报。

编辑3:解决了。 这是Windows“勒索软件保护”。 不允许 Python 写入我的工作目录。

解决了。 这是Windows“勒索软件保护”。 不允许 Python 写入我的工作目录。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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