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

时间:2019-07-11 20:01:51

标签: python

我正在尝试做有史以来最简单的事情,但我无法使其正常工作。

我在我的工作目录中,我们称之为“ 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写入我的工作目录。

1 个答案:

答案 0 :(得分:0)

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