This question already has answers here:
Python raising FileNotFoundError for file name returned by os.listdir
(3 answers)
Closed 2 months ago.
I'm trying to rename the files in a folder, but I can't. This is my code:
import os
directory = r"C:\Users\jhonatta-silva\Downloads"
for file in os.listdir(directory):
if file.startswith("PBI"):
os.rename(file, file.strip("0123456789 "))
and I receive an error like this:
[WinError 2] The system cannot find the file specified: 'PBIItemsResults132.csv' -> 'PBIItemsResults132.csv'
You have to add the directory to the names in the os.rename() call.
import os
directory = r"C:\Users\jhonatta-silva\Downloads"
for file in os.listdir(directory):
if file.startswith("PBI"):
os.rename(os.path.join(directory, file), os.path.join(file.strip("0123456789 ")))
Instead of using os.listdir() and then file.startswith(), you can use glob.glob() to just process files beginning with that prefix. It returns full paths when the argument is a full path.
import os, glob
directory = r"C:\Users\jhonatta-silva\Downloads"
for file in glob.glob(os.path.join(directory, "PBI*")):
os.rename(file, file.rstrip("0123456789 "))
Related
This question already has answers here:
What exactly is current working directory?
(5 answers)
open() gives FileNotFoundError / IOError: '[Errno 2] No such file or directory'
(8 answers)
Closed 17 days ago.
I have a folder where want to delete the files on it when they reaches 5 files (or if they are older than 5 days). so far I have tried this:
path_file = r'C:\...\new folder'
files = os.listdir(path_file)
for file in files [:5]:
os.remove(file)
This code gives the next error:
os.remove(file)
FileNotFoundError: [WinError 2] The system cannot find the file specified file
Add path to the filename
path= r'C:\...\new folder'
files = os.listdir(path_file)
for file in files [:5]:
os.remove(path + "\\" + file)
This question already has answers here:
Python raising FileNotFoundError for file name returned by os.listdir
(3 answers)
Closed 2 months ago.
I'm developing a loop where each csv in a specified directory is re-sampled and then exported into a new file. I'm getting a FileNotFoundError despite trying with various folders and using exact folder paths.
# Specify folder name
serial = '015'
# Specify directory (note - '...' substitute for the full path used back to the drive letter)
root_dir = '...\\CleanTemps\\{}\\'.format(str(serial))
#loop
for filename in os.listdir(root_dir):
if filename.endswith('.csv'):
print(filename)
# Pull in the file
df = pd.read_csv(filename)
This prints a list of the eight files .csv files in that folder. However, when using the following code to pull in the file (one-by-one as a df to modify, I receive the FileNotFoundError:
#loop
for filename in os.listdir(root_dir):
if filename.endswith('.csv'):
# Pull in the file
df = pd.read_csv(filename)
The path to your file is compose from root_path + your file name, you can use :
from pathlib import Path
root_path = Path(root_dir)
for filename in os.listdir(root_path):
if filename.endswith('.csv'):
# Pull in the file
df = pd.read_csv(root_path/filename)
or you can use:
for filepath in root_path.glob("*.csv"):
df = pd.read_csv(filepath)
You must provide the full (or relative) path to the file, not just its name, this path is based on the root of your files, and you can use os.path.join to build it:
df = pd.read_csv(os.path.join(root_dir, file_name))
This question already has answers here:
Python raising FileNotFoundError for file name returned by os.listdir
(3 answers)
Closed 2 months ago.
I'm developing a loop where each csv in a specified directory is re-sampled and then exported into a new file. I'm getting a FileNotFoundError despite trying with various folders and using exact folder paths.
# Specify folder name
serial = '015'
# Specify directory (note - '...' substitute for the full path used back to the drive letter)
root_dir = '...\\CleanTemps\\{}\\'.format(str(serial))
#loop
for filename in os.listdir(root_dir):
if filename.endswith('.csv'):
print(filename)
# Pull in the file
df = pd.read_csv(filename)
This prints a list of the eight files .csv files in that folder. However, when using the following code to pull in the file (one-by-one as a df to modify, I receive the FileNotFoundError:
#loop
for filename in os.listdir(root_dir):
if filename.endswith('.csv'):
# Pull in the file
df = pd.read_csv(filename)
The path to your file is compose from root_path + your file name, you can use :
from pathlib import Path
root_path = Path(root_dir)
for filename in os.listdir(root_path):
if filename.endswith('.csv'):
# Pull in the file
df = pd.read_csv(root_path/filename)
or you can use:
for filepath in root_path.glob("*.csv"):
df = pd.read_csv(filepath)
You must provide the full (or relative) path to the file, not just its name, this path is based on the root of your files, and you can use os.path.join to build it:
df = pd.read_csv(os.path.join(root_dir, file_name))
This question already has answers here:
How should I write a Windows path in a Python string literal?
(5 answers)
Closed 4 years ago.
So I am using a function in python which is being called in Robotframework to copy a file from a source to destination
I have used os.path.join() and os.listdir() and os.path.normpath() to get access to the folder and copy using shutil
But everytime I get this error
WindowsError: [Error 3] The system cannot find the path specified: '\\10.28.108.***\\folder\\folder2\\out/*.*'
My code
from pathlib import Path
import shutil
import os
#filename = Path ("\\10.28.108.***\folder\folder2\out\001890320181228184056-HT.xml")
source = os.listdir("\\10.28.108.***\folder\folder2\out")
destination = "\\10.28.108.***\folder\folder2\"
for files in source :
if files.endswith(".xml"):
shutil.copy(files, destination)
By this you can read your file.
filename = secure_filename(file_name.filename)
file_split = os.path.splitext(filename)
filename = file_split[0] + '__' + str(uuid.uuid4()) + file_split[1]
filepath = os.path.join(dest_dir, filename)
syspath = os.path.join(upload_dir, filepath)
file_name.save(syspath)
first thing here that check if you can access this folder(\10.28.108.\folder\folder2\out) from your file explorer
The other thing is you have to specify two slash if you are accessing remote folder below is example:
source = os.listdir(r"\\10.28.108.xxx\folder\folder2\out")
This question already has answers here:
Bug in Python Renaming Program.....No such file or Directory (Fnmatch)
(2 answers)
Closed 6 years ago.
I have 7 files in my web directory like this :
I'm trying to scan through all my files in that folder and rename a certain files.
I have
import os
path = '/Users/username/Desktop/web'
for filename in os.listdir(path):
if filename.startswith("test"):
print 'found'
os.rename(filename, filename.replace("test_", " "))
else:
continue
After run it,
python scan_dir.py
I got
found
Traceback (most recent call last):
File "scan_dir.py", line 9, in <module>
os.rename(filename, filename.replace("test_", " "))
OSError: [Errno 2] No such file or directory
Any hints on what I did wrong ?
When you rename you should use the full path:
import os
path = '/Users/name/Desktop/web'
for filename in os.listdir(path):
if filename.startswith("test"):
print 'found'
os.rename(os.path.join(path, filename), os.path.join(path, filename.replace("test_", " ")))
else:
continue
In your current code what you do use try to rename the file test_3.jpg (from your example) in the current directory, which probably don't exists.
BTW, I would concider using the glob function instead.