简体   繁体   中英

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

I'm trying to do the simplest thing ever and I can't get it to work.

I'm in my working directory, let's call it 'WorkDir' and this is it: C:\\WorkDir

I want to create:

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'

I don't understand this error. Of course it can't find the file, it doesn't exist. Obviously I'm doing something wrong, but can't figure it out.

I also tried to use the period '.' to denote working directory, but that doesn't work either.

None of these work:

# 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'

As far as I can tell I'm copying from stackoverflow posts word for word. Can't figure it out.

The strange thing is I can create a new directory directly in the C: Drive, like:

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

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

But if I try to do anything in the working directory I get the error.

edit: Full error:

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'

EDIT 2, FOUND SOMETHING: So i just noticed at the top of my notebook, that autosave failed. Might have something to do with this. Let me investigate. Sorry for the false alarm.

Edit 3: Solved. It was windows 'ransomware protection'. Python wasn't allowed to write to my working directory.

Solved. It was windows 'ransomware protection'. Python wasn't allowed to write to my working directory.

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