S
S
SemenAnnigilator2021-09-29 04:19:19
Python
SemenAnnigilator, 2021-09-29 04:19:19

How to rename a file using python?

The code:

paths = "/content/train2017"
midis = os.listdir(paths)
    for j in midis: 
        for i in imgsids:
            os.renames("/content/train2017/" + j, str(i) + ".jpg")

Error code:
---------------------------------------------------------------------------
    FileNotFoundError                         Traceback (most recent call last)
    <ipython-input-106-b57cb06c2dff> in <module>()
         14 for j in midis:
         15     for i in imgsids:
    ---> 16         os.renames("/content/train2017/" + j, str(i) + ".jpg")
         17 catIds = coco.getCatIds(catNms=['person', "car"])
         18 imgIds = coco.getImgIds(catIds=catIds );
    
    /usr/lib/python3.7/os.py in renames(old, new)
        268     if head and tail and not path.exists(head):
        269         makedirs(head)
    --> 270     rename(old, new)
        271     head, tail = path.split(old)
        272     if head and tail:
    
    FileNotFoundError: [Errno 2] No such file or directory: '/content/train2017/000000211351.jpg' -> '522418.jpg'

A photo of the directory and the error code, so that there is more understanding.
6153bf1316da1815823804.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dmshar, 2021-09-29
@dmshar

And what, exactly, is incomprehensible?
I hope you have no problems with English? They clearly wrote to you:
No such file or directory: '/content/train2017/000000211351.jpg' -> '522418.jpg'
Which word in this message is not clear? But what kind of file or directory you don’t have - we don’t know this, you need to look at the computer on which you are performing this task.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question