[Tutor] Tutoring help automating with python

Cameron Simpson cs at cskk.id.au
Wed Jul 26 19:45:01 EDT 2023


On 26Jul2023 15:17, Fatima Mehak <bossladyofthefuture at gmail.com> wrote:
>I've been getting errors when trying to run the below text file. I have
>indicated the error at the bottom for review. Can you please assist?
[...]
>```
>        targetdir = "../output/"
>        srcdir = "../input/"
>        for file in os.listdir(srcdir):
>             resize_rename_rotate(file,targetdir)
>```
>
>```
>#*Traceback (most recent call last):
>#  File "/home/student-03-61dc276e6247/./fix_image.py", line 23, in <module>
>#    for file in os.listdir(srcdir):
>#FileNotFoundError: [Errno 2] No such file or directory: '../input/'
>```

This means that there's no such directory `../input/`. Because that is a 
relative path, it is important that when you invoke Python that it is 
_running_ in the right directory for that path to work.

Try putting this:

     import os
     print(os.getcwd())

before the `for...` line. That will tell you the current working 
directory Python is using, and hopefully that will make clear why the 
relative path is not working - the relative path must be valid from that 
directory from `os.getcwd()`.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Tutor mailing list