Error while renaming files with os.rename() in Python - python

import os
for filename in os.listdir("C:/Users/Awesome/Music"):
if filename.endswith("lyrics.mp3"):
os.rename(filename,filename[0 : len(filename)-11]+".mp3")
The code above returns the error
File "c:/python/lyrics-pop.py", line 6, in <module>
os.rename(filename,filename[0 : len(filename)-11]+".mp3")
FileNotFoundError: [WinError 2] The system cannot find the file specified: '2 Chainz - Bigger Than You (feat Drake Quavo) lyrics.mp3' -> '2 Chainz - Bigger Than You (feat Drake Quavo).mp3'
"""
I have made sure that no other program is accessing the .mp3 files and removed the readonly attribute. What could be causing this?

Probably, the issue is that you are passing relative path to os.rename, add dir to file path, like this:
import os
dir = "C:/Users/Awesome/Music"
for filename in os.listdir(dir):
if filename.endswith("lyrics.mp3"):
os.rename(os.path.join(dir,filename),os.path.join(dir,filename[0 : len(filename)-11])+".mp3")

This is because python can not find the file from where this program is running, since full path is not given.
You can do it as follows:
import os
filedir = "C:/Users/Awesome/Music"
for filename in os.listdir(filedir):
if filename.endswith("lyrics.mp3"):
filepath = os.path.join(filedir, filename)
new_file = os.path.join(filedir, filename[0 : len(filename)-11]+".mp3")
os.rename(filepath, new_file)

As suggested in the comments, the problem seems to be the relative path of the files.
You can use glob, which will give you the full path, i.e.:
from glob import glob
from os import rename
for f in glob("C:/Users/Awesome/Music/*lyrics.mp3"):
rename(f, f[0 : len(f)-11]+".mp3")

Related

Receiving "no such file or directory" error within a for loop - recognizes file initially

Code below:
for WorkingFile in os.listdir(path):
print(WorkingFile)
xlsx = pd.ExcelFile(WorkingFile)
returns this:
ChetworthPark_Test.xlsx
FileNotFoundError: [Errno 2] No such file or directory: 'ChetworthPark_Test.xlsx'
So it's printing the file name (demonstrating that it recognizes the path), but then not passing it to the variable "xlsx" after. Any ideas on where I'm going wrong? For more context, I'm running this in Google Colab.
os.listdir returns the file name not the path. So you need to prepend the path:
for fn in os.listdir(path):
do_something(f"{path}/{fn}")
As pointed out in a comment, / for paths is not universal, so we have os.path.join:
from os.path import join
for fn in os.listdir(path):
fn = join(path, fn) # handles / or \
do_something(fn)
However for a fair while now we've had pathlib.Path which makes this much easier:
from pathlib import Path
for fn in os.listdir(path):
do_something(Path(path) / fn)
or, more naturally with pathlib:
from pathlib import Path
for fn in Path("/path/to/look/at").expanduser().resolve().glob("*"):
if not fn.is_file():
continue
do_something(fn)
(note that I've also handled expanding things like ~/some-file and simplifying the path here)

file does not exist when I want to move it

I'm preprocessing datas to apply deep learning on audio files. I have a directory of audio file (.wav) with differents length and my goal is to use 0 padding in order to have only 10 secondes files duration. I also want to move move files that are longer than 10 secondes in an other directory:
from pydub import AudioSegment
import os
import shutil
path_to_sound = r'my\path\to\sound'
path_to_export = r'my\path\to\export'
path_for_too_long = r'my\path\to\other_directory'
pad_ms= 10000
file_names=os.listdir(path_to_sound)
print(file_names) # ['30368.wav', '41348.wav', '42900.wav', '42901.wav', '42902.wav']
for name in file_names:
audio= AudioSegment.from_wav(os.path.join(path_to_sound, name))
if pad_ms > len(audio):
shutil.move(name,path_for_too_long )
else:
silence = AudioSegment.silent(duration=pad_ms-len(audio)+1)
padded = audio + silence
padded.export(os.path.join(path_to_export, name), format = 'wav')
When running this I got the following errors:
FileNotFoundError: [WinError 2] The specified file can not be found : '41348.wav' -> 'C:\\Users\\path_for_too_long\\41348.wav'
...
FileNotFoundError: [Errno 2] No such file or directory: '41348.wav'
I think the error is due to the way i'm using shutil.move()
shutil.move expects the full path, here you are providing the name which corresponds to the filename and not the file path.
You have to update the line:
shutil.move(name, path_for_too_long)
to:
shutil.move(os.path.join(path_to_sound, name), path_for_too_long)
Remark: if you are using Python 3.4+ you can use pathlib instead of os to handle paths. You can find 2 articles on this:
Why you should be using pathlib
No really, pathlib is great
before u delete,try to findout if is exist.
import os
path = ''
if os.path.exists(path):
func_to_delete()

Remove certain strings from filenames inside a folder

I have a folder that contains a bunch of text files.
32167.pdf.txt
20988.pdf.txt
45678.pdf.txt
:
:
99999.pdf.txt
I would like to remove ".pdf" from all the filenames inside that folder (As showed below).
32167.txt
20988.txt
45678.txt
:
:
99999.txt
I tried to use os to do it but throwing me an error FileNotFoundError: [Errno 2] No such file or directory: '.txt' -> '.txt'
Here is my code:
for filename in os.listdir('/Users/CodingStark/folder/'):
os.rename(filename, filename.replace('.pdf', ''))
I am wondering are there any other ways to achieve this instead of using os? Or os is the fastest way to do it? Thank you!!
I think I know what's wrong. filename will contain just the file's name, not a complete path. Try this:
dir = '/Users/CodingStark/folder/'
for filename in os.listdir(dir):
os.rename(dir + filename, dir + filename.replace('.pdf', ''))
You should really use pathlib when working with paths. It has alternatives for most of the os functions and it just makes more sense to use and is definitely more portable.
To the task in hand:
from pathlib import Path
root = Path('/Users/CodingStark/folder/')
for path in root.glob("*.pdf.txt"):
path.rename(path.with_name(path.name.replace(".pdf", "")))
# or:
# path.with_suffix('').with_suffix(".txt")
# or:
# str(path).replace(".pdf", "")

Recovering filenames from a folder in linux using python

I am trying to use the listdir function from the os module in python to recover a list of filenames from a particular folder.
here's the code:
import os
def rename_file():
# extract filenames from a folder
#for each filename, rename filename
list_of_files = os.listdir("/home/admin-pc/Downloads/prank/prank")
print (list_of_files)
I am getting the following error:
OSError: [Errno 2] No such file or directory:
it seems to give no trouble in windows, where you start your directory structure from the c drive.
how do i modify the code to work in linux?
The code is correct. There should be some error with the path you provided.
You could open a terminal and enter into the folder first. In the terminal, just key in pwd, then you could get the correct path.
Hope that works.
You could modify your function to exclude that error with check of existence of file/directory:
import os
def rename_file():
# extract filenames from a folder
#for each filename, rename filename
path_to_file = "/home/admin-pc/Downloads/prank/prank"
if os.exists(path_to_file):
list_of_files = os.listdir(path_to_file)
print (list_of_files)

Python:writing files in a folder to zipfile and compressing it

I am trying to zip and compress all the files in a folder using python. The eventual goal is to make this occur in windows task scheduler.
import os
import zipfile
src = ("C:\Users\Blah\Desktop\Test")
os.chdir=(src)
path = (r"C:\Users\Blah\Desktop\Test")
dirs = os.listdir(path)
zf = zipfile.ZipFile("myzipfile.zip", "w", zipfile.ZIP_DEFLATED,allowZip64=True)
for file in dirs:
zf.write(file)
Now when I run this script I get the error:
WindowsError: [Error 2] The system cannot find the file specified: 'test1.bak'
I know it's there since it found the name of the file it can't find.
I'm wondering why it is not zipping and why this error is occurring.
There are large .bak files so they could run above 4GB, which is why I'm allowing 64 bit.
Edit: Success thanks everyone for answering my question here is my final code that works, hope this helps my future googlers:
import os
import zipfile
path = (r"C:\Users\vikram.medhekar\Desktop\Launch")
os.chdir(path)
dirs = os.listdir(path)
zf = zipfile.ZipFile("myzipfile.zip", "w", zipfile.ZIP_DEFLATED,allowZip64=True)
for file in dirs:
zf.write(os.path.join(file))
zf.close()
os.listdir(path) returns names relative to path - you need to use zf.write(os.path.join(path, file)) to tell it the full location of the file.
As I said twice in my comments:
Python is not looking for the file in the folder that it's in, but in the current working directory. Instead of
zf.write(file)
you'll need to
zf.write(path + os.pathsep + file)

Categories

Resources