Error question regarding rename file in Python folder. FileNotFoundError

Asked 1 weeks ago, Updated 1 weeks ago, 1 views

There is a problem if the name is too long in the folder, so I am working on a code that changes it at once. An error has occurred.

import os
import pathlib

p_dir =  './data/'

for file in os.listdir(p_dir):
    file_name = file
    oldname = str(file_name)
    newname1 = file.split('_')[5] + ''
    newname = str(newname1)
    os.rename(oldname, newname)

I tried it like this, but I don't think there's a problem

Traceback (most recent call last):
  File "d:/Coding/name/", line 15, in <module>
    os.rename(oldname, newname)
FileNotFoundError: [WinError 2] 指定されたファイルが見つかりません。: '' -> ''

定さたファイルつかり 지정된まんせ : 파일을 찾을습니다: The specified file could not be found.

I see this error. Is there any way?


2022-09-20 10:54

1 Answers

We only have the file name, so shouldn't we add the path name?

oldname = p_dir + file_name

2022-09-20 10:54

If you have any answers or tips

© 2022 pinfo. All rights reserved.