Reputation: 1844
I have .wav files in the directory "dataset"
import os
dataset_path = 'C:/dataset'
def change_filenames(dataset_path):
i = 0 # target filename
for old_name in os.listdir(dataset_path):
os.rename(old_name, str(i) + '.wav')
i+=1
change_filenames(dataset_path)
Error: FileNotFoundError: [WinError 2] The system cannot find the file specified: 'sound1.wav' -> '0.wav'
What is the error mean? The file is in the directory and is reachable by the code, why not system?
Upvotes: 1
Views: 81
Reputation: 131
As Zerodf points out, the problem is likely that your current working directory is not the same as the dataset_path
directory you specified. So when os.rename
goes to rename sound1.wav
, it looks for it in your current working directory (which might not be the same as dataset_path
), can't find it, and throws that error.
Since your dataset_path
variable contains an absolute path, you can make sure that everything that os.rename
tries to rename is an absolute path by tacking each filename onto dataset_path
using os.path.join(). That is, change
os.rename(old_name, str(i) + '.wav')
to
os.rename(os.path.join(dataset_path, old_name), os.path.join(dataset_path, str(i) + '.wav'))
Then when os.rename
goes to rename sound1.wav
, it will look for C:/dataset/sound1.wav
, which it should find without a problem.
Also note that you can see what your current working directory is with the getcwd()
function in os
.
import os
print(os.getcwd())
Upvotes: 2