How do I fix a shutil.copy issue? - python

I keep getting an error that says
"IOError: [Errno 2] No such file or directory: 'C:\Temp\test2_empty\Storage\Poly1.kml'"
What I want to do is copy a file from a directory move it to a temporary storage folder and rename the file and then move that file to another folder. What is the best way to fix this issue?
from qgis.core import*
import glob, os, shutil, time, qgis
path = r"C:\Temp\test2_kml"
dest = r"C:\Temp\test2_empty"
storage = r"C:\Temp\test2_empty\Storage"
for root,d_names,f_names in os.walk(path):
if not f_names:
continue
prefix = os.path.basename(root)
for f in f_names:
if f.endswith('.kml'):
src = os.path.join(root,f)
print("...")
print(time.strftime('%m/%d/%Y', time.gmtime(os.path.getmtime(src))))
print(os.path.realpath(src))
print(f)
shutil.copy2(src, storage)
for root2,d_names2,f_names2 in os.walk(storage):
for f2 in f_names2:
src2= os.path.join(root2,f2)
os.rename(os.path.join(root2,f2), os.path.join(root2, "{}_{}".format(prefix,f2)))
shutil.move(src2, dest)

Create your destination directory - os.makedirs(storage) before calling shutil copy.
If you want to accept the pre-existence of this directory, you can:
for python 3.2+ - add keyword argument exist_ok=True
for python <3.2 - add try / except block for OSError exception.

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)

Copy file with specific name from one folder to another in python

I am trying to copy specific files from one folder to another but I get an error I don't understand why :
import os
import shutil
def setPath_getData():
# Set up folders for data
newpath = r'userdata'
if not os.path.exists(newpath):
os.makedirs(newpath)
os.makedirs('userdata/sleep')
os.makedirs(r'userdata/distance')
os.makedirs(r'userdata/steps')
os.makedirs(r'userdata/lightly')
os.makedirs(r'userdata/mod')
os.makedirs(r'userdata/sedentary')
os.makedirs(r'userdata/very')
os.makedirs(r'userdata/heart-rate-zone')
os.makedirs(r'userdata/heart-rate')
# Get data from fitbit
filenames = os.listdir("user-site-export")
unique_filenames = set()
for f in filenames:
unique_filenames.add(f.split("-")[0])
source = os.listdir('user-site-export/')
dest = '/userdata/sleep/'
for file in source:
if file.startswith('sleep'):
shutil.copy(file, dest)
#ls userdata/
print("Data loaded successfully")
setPath_getData()
the error it gives is :
FileNotFoundError: [Errno 2] No such file or directory: 'sleep-2020-01-09.json'
So it looks like it is fetching the correct files but it does not copy them to dest. Any ideas why ?
You must specify the source path before the file variable in the copy command:
shutil.copy(os.path.join("user-site-export", file), dest)

why does python say it cant find the path specified when it made the path?

I made this program yesterday because I am using py2exe, so what this program does is it zips up the folder created by py2exe and names it to app4export so I can send it to my friends. I also added in where if i already have a zip file called app4export then it deletes it before hand, it worked yesterday but now today I get the error
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:\\Users\\severna\\Desktop\\Non_Test_Python_Files\\app4export'
but python made this location so I dont get why it cant find it later?
import os
import zipfile
import shutil
def zip(src, dst):
zf = zipfile.ZipFile("%s.zip" % (dst), "w", zipfile.ZIP_DEFLATED)
abs_src = os.path.abspath(src)
for dirname, subdirs, files in os.walk(src):
for filename in files:
absname = os.path.abspath(os.path.join(dirname, filename))
arcname = absname[len(abs_src) + 1:]
print('zipping %s as %s' % (os.path.join(dirname, filename),
arcname))
zf.write(absname, arcname)
zf.close()
source=r"C:\Users\severna\Desktop\Non_Test_Python_Files\dist"
destination=r"C:\Users\severna\Desktop\Non_Test_Python_Files\app4export"
shutil.rmtree(str(destination))
try:
zip(str(source), str(destination))
shutil.rmtree(str(source))
except FileNotFoundError:
print("Source cannot be zipped as it does not exist!")
Your code creates the file C:\Users\severna\Desktop\Non_Test_Python_Files\app4export.zip, but you try to remove the directory C:\Users\severna\Desktop\Non_Test_Python_Files\app4export
So just before the try-block you have
shutil.rmtree(str(destination))
which will throw an FileNotFoundError if the path do not exist. And when you hit that line of code, you still havent created the path. The reason it might have worked yesterday was that you mayby had that path.
after discussion with Cleared I found out that I needed i file extension because it was a file and shutil.rmtree doesnt remove files it removes directories so I need to use this code instead
os.remove(str(destination)+".zip")

Renaming a single file in python

I'm trying to rename an audio file but I keep getting OSError: [Errno 2] No such file or directory.
In my program, each user has a directory that holds the users files. I obtain the path for each user by doing the following:
current_user_path = os.path.join(current_app.config['UPLOAD_FOLDER'], user.username)
/Users/johnsmith/Documents/pythonprojects/project/files/john
Now I want to obtain the path for the existing file I want to rename:
current_file_path = os.path.join(current_user_path,form.currentTitle.data)
/Users/johnsmith/Documents/pythonprojects/project/files/john/File1
The path for the rename file will be:
new_file_path = os.path.join(current_user_path, form.newTitle.data)
/Users/johnsmith/Documents/pythonprojects/project/files/john/One
Now I'm just running the rename command:
os.rename(current_file_path, new_file_path)
you can use os.rename for rename single file.
to avoid
OSError: [Errno 2] No such file or directory.
check if file exist or not.
here is the working example:
import os
src = "D:/test/Untitled003.wav"
dst = "D:/test/Audio001.wav"
if os.path.isfile(src):
os.rename(src, dst)
If the OS says there's no such file or directory, that's the gospel truth. You're making a lot of assumptions about where the file is, constructing a path to it, and renaming it. It's a safe bet there's no such file as the one named by current_file_path, or no directory to new_file_path.
Try os.stat(current_file_path), and similarly double-check the new file path with os.stat(os.posixpath.dirname(new_file_path)). Once you've got them right, os.rename will work if you've got permissions.
Try changing the current working directory to the one you want to work with. This code below should give you a simple walk through of how you should go about it:
import os
print (os.getcwd())
os.chdir(r"D:\Python\Example")
print (os.getcwd())
print ("start")
def rename_files():
file_list= os.listdir(r"D:\Python\Example")
print(file_list)
for file_name in file_list:
os.rename(file_name,file_name.translate(None,"0123456789")) rename_files()
print("stop")
print (os.getcwd())

Python. IOError: [Errno 13] Permission denied: when i'm copying file

I have two folders: In, Out - it is not system folder on disk D: - Windows 7. Out contain "myfile.txt" I run the following command in python:
>>> shutil.copyfile( r"d:\Out\myfile.txt", r"D:\In" )
Traceback (most recent call last):
File "<pyshell#39>", line 1, in <module>
shutil.copyfile( r"d:\Out\myfile.txt", r"D:\In" )
File "C:\Python27\lib\shutil.py", line 82, in copyfile
with open(dst, 'wb') as fdst:
IOError: [Errno 13] Permission denied: 'D:\\In'
What's the problem?
Read the docs:
shutil.copyfile(src, dst)
Copy the contents (no metadata) of the file named src to a file
named dst. dst must be the complete target file name; look at copy()
for a copy that accepts a target directory path.
use
shutil.copy instead of shutil.copyfile
example:
shutil.copy(PathOf_SourceFileName.extension,TargetFolderPath)
Use shutil.copy2 instead of shutil.copyfile
import shutil
shutil.copy2('/src/dir/file.ext','/dst/dir/newname.ext') # file copy to another file
shutil.copy2('/src/file.ext', '/dst/dir') # file copy to diff directory
I solved this problem, you should be the complete target file name for destination
destination = pathdirectory + filename.*
I use this code fir copy wav file with shutil :
# open file with QFileDialog
browse_file = QFileDialog.getOpenFileName(None, 'Open file', 'c:', "wav files (*.wav)")
# get file name
base = os.path.basename(browse_file[0])
os.path.splitext(base)
print(os.path.splitext(base)[1])
# make destination path with file name
destination= "test/" + os.path.splitext(base)[0] + os.path.splitext(base)[1]
shutil.copyfile(browse_file[0], destination)
First of all, make sure that your files aren't locked by Windows, some applications, like MS Office, locks the oppened files.
I got erro 13 when i was is trying to rename a long file list in a directory, but Python was trying to rename some folders that was at the same path of my files. So, if you are not using shutil library, check if it is a directory or file!
import os
path="abc.txt"
if os.path.isfile(path):
#do yor copy here
print("\nIt is a normal file")
Or
if os.path.isdir(path):
print("It is a directory!")
else:
#do yor copy here
print("It is a file!")
Visual Studio 2019
Solution : Administrator provided full Access to this folder "C:\ProgramData\Docker"
it is working.
ERROR: File IO error seen copying files to volume: edgehubdev. Errno: 13, Error Permission denied : [Errno 13] Permission denied: 'C:\ProgramData\Docker\volumes\edgehubdev\_data\edge-chain-ca.cert.pem'
[ERROR]: Failed to run 'iotedgehubdev start -d "C:\Users\radhe.sah\source\repos\testing\AzureIotEdgeApp1\config\deployment.windows-amd64.json" -v' with error: WARNING! Using --password via the CLI is insecure. Use --password-stdin.
ERROR: File IO error seen copying files to volume: edgehubdev. Errno: 13, Error Permission denied : [Errno 13] Permission denied: 'C:\ProgramData\Docker\volumes\edgehubdev\_data\edge-chain-ca.cert.pem'
use
> from shutil import copyfile
>
> copyfile(src, dst)
for src and dst use:
srcname = os.path.join(src, name)
dstname = os.path.join(dst, name)
This works for me:
import os
import shutil
import random
dir = r'E:/up/2000_img'
output_dir = r'E:/train_test_split/out_dir'
files = [file for file in os.listdir(dir) if os.path.isfile(os.path.join(dir, file))]
if len(files) < 200:
# for file in files:
# shutil.copyfile(os.path.join(dir, file), dst)
pass
else:
# Amount of random files you'd like to select
random_amount = 10
for x in range(random_amount):
if len(files) == 0:
break
else:
file = random.choice(files)
shutil.copyfile(os.path.join(dir, file), os.path.join(output_dir, file))
Make sure you aren't in (locked) any of the the files you're trying to use shutil.copy in.
This should assist in solving your problem
I avoid this error by doing this:
Import lib 'pdb' and insert 'pdb.set_trace()' before 'shutil.copyfile', it would just like this:
import pdb
...
print(dst)
pdb.set_trace()
shutil.copyfile(src,dst)
run the python file in a terminal, it will execute to the line 'pdb.set_trace()', and now the 'dst' file will print out.
copy the 'src' file by myself, and substitute and remove the 'dst' file which has been created by the above code.
Then input 'c' and click the 'Enter' key in the terminal to execute the following code.
well the questionis old, for new viewer of Python 3.6
use
shutil.copyfile( "D:\Out\myfile.txt", "D:\In" )
instead of
shutil.copyfile( r"d:\Out\myfile.txt", r"D:\In" )
r argument is passed for reading file not for copying

Categories

Resources