3

I'm trying to write a binary file i'm downloading from The Movie Database. I can successfully save the file if it's on the same folder the .py file is, but cannot save it on a location outside it even after the containing directory is created by the script and using absolute path:

FileNotFoundError: [Errno 2] No such file or directory: '/home/gabriel/Desktop/movies/w185/tt2024544.jpeg'

Pice of code:

filepath    =   os.path.join(webDir,size,imdbid + '.' + filetype);
f           =   open(filepath, "wb");

I'm puzzled. Any tips?

Thanks!

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Gabriel
  • 5,453
  • 14
  • 63
  • 92
  • What code is causing that exception? My best guest is that you're not opening the file for writing.... – Jon Clements Mar 18 '15 at 03:38
  • Can you [edit] your question to include that, as well as how `filepath` is assigned? – Jon Clements Mar 18 '15 at 03:40
  • Dang, did not see the edit file, correcting... – Gabriel Mar 18 '15 at 03:42
  • Are you sure the path is correct? Are you sure all subfolders, i.e. movies and w185 already exist? Linux is case-sensitive so check to ensure you are using the correct casing. – Reticulated Spline Mar 18 '15 at 03:42
  • @Gabriel do all subdirectories exist? You need to ensure that /home/gabriel/Desktop/movies/w185/` exists... – Jon Clements Mar 18 '15 at 03:42
  • Yes indeed. As i stated, the directory is even created by my script if does not exist. – Gabriel Mar 18 '15 at 03:44
  • How are you creating the directory? Also - for a sanity check... you're sure the directory does exist? (ie, you can cd into it inside a shell...and maybe check that something like `echo test > somefilename` works as the same user the Python process will be running as?) – Jon Clements Mar 18 '15 at 03:49
  • Oh, i'm at work and dont have access to the code, but yes, the directory exists and i can access it via CLI or Nautilus. I will check permissions tho. – Gabriel Mar 18 '15 at 16:15
  • The directories are accessed by CLI and the permissions are right (drwxr-xr-x). Even executing the script as root throws the same error. – Gabriel Mar 20 '15 at 02:06
  • If it only works fine if they're in the same folder, you might be saving relative to the current directory. Also the `os.path.join` docs say "If any component is an absolute path, all previous path components will be discarded." Make sure none of the arguments have a slash in front of them. – Zenul_Abidin Jan 07 '20 at 13:12
  • Does this answer your question? [Trying to use open(filename, 'w' ) gives IOError: \[Errno 2\] No such file or directory if directory doesn't exist](https://stackoverflow.com/questions/18758673/trying-to-use-openfilename-w-gives-ioerror-errno-2-no-such-file-or-dire) – Karl Knechtel Jul 19 '23 at 03:04

1 Answers1

1

I had the same issue and it turned out that a directory in my absolute path was not existing. One can easily check with:

import os
path = os.path.split(filepath)[0]
print('check for path', os.path.exists(path))
Markus Dutschke
  • 9,341
  • 4
  • 63
  • 58