0

Trying to simply sort two directories, and it works fine the first time, but when I call it a second time (last line below, for tallFiles), it throws a 'No such file or directory error, even though it correctly lists the first sorted file in that directory.

import os
import datetime
from pathlib import Path

wallDir = Path('/Users/leigh/Dropbox/Photos/Wallpaper')
tallDir = Path('/Users/leigh/Dropbox/Photos/Wallpaper/Tall')

wallFiles = sorted(os.listdir(wallDir), key=lambda t: -os.stat(t).st_mtime)
tallFiles = sorted(os.listdir(tallDir), key=lambda t: -os.stat(t).st_mtime)

Throws this error:

leigh@outpost31 Wallpaper % python3 ~/Dropbox/Development/rename.py
Traceback (most recent call last):
  File "/Users/leigh/Dropbox/Development/rename.py", line 9, in <module>
    tallFiles = sorted(os.listdir(tallDir), key=lambda t: -os.stat(t).st_mtime)
  File "/Users/leigh/Dropbox/Development/rename.py", line 9, in <lambda>
    tallFiles = sorted(os.listdir(tallDir), key=lambda t: -os.stat(t).st_mtime)
FileNotFoundError: [Errno 2] No such file or directory: 'holjfmgopwk81.jpg'

The file and path absolutely exist...

deleteno5
  • 29
  • 3
  • The file doesn't exist *in the current directory*. You'd need to pass the directory name too, or `chdir` into it. There are existing questions that cover this, and I think they'll fully answer your question, but LMK if not. – wjandrea Aug 19 '22 at 19:28
  • `key=lambda t: -os.stat(t).st_mtime` You're only passing the bare filename into `os.stat()`, but you need the directory name also, because that file does not exist in the current working directory. – John Gordon Aug 19 '22 at 19:28
  • 1
    Oh, for heaven's sake...thank you. – deleteno5 Aug 19 '22 at 19:32

0 Answers0