1

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.

Guy Coder
  • 24,501
  • 8
  • 71
  • 136
SCool
  • 3,104
  • 4
  • 21
  • 49

1 Answers1

4

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

SCool
  • 3,104
  • 4
  • 21
  • 49
  • Did you find a robust solution to this? – toonarmycaptain Mar 02 '20 at 18:11
  • If you want a robust solution don't use Windows. That's all I got. Did you try adding a backslash to the very end of the path? – SCool Mar 03 '20 at 00:30
  • I believe so? I assume `Path` does whatever it needs to. Blanket allowing python.exe through the block on Controlled Folder Access solved for me. – toonarmycaptain Mar 03 '20 at 05:20
  • Thanks to confirm that. I created a "working" folder in C:/ to do coding, now it has this problem. I have to move to "personal folder", as in C:/Users/xxxx/ , and the same code works well in this personal folder. – Ben Lin Dec 07 '21 at 15:28