Errno 22 invaild (wb) or filename. Python shutil.copy - python

I'm beginner in python and I have a problem with this script:
import errno import shutil import os
def copystuff(src, dest):
if os.path.isfile(src):
dest_dirname = os.path.dirname(src)
if not os.path.isdir(dest_dirname):
os.makedirs(dest_dirname)
shutil.copy2(src, dest)
else:
shutil.copytree(src, dest)
copystuff('C:\\Downloads\\index.html', 'J:\\include\\')
Where J it's FlashDriveUSB, and I'm using Python 2.7.
When I'm launching this I got something like that:
C:\Python27>python copy_file.py
Traceback (most recent call last):
File "copy_file.py", line 24, in <module>
copystuff('C:\\Downloads\index.html', 'D:\\include\\')
File "copy_file.py", line 20, in copystuff
shutil.copy2(src, dest)
File "C:\Python27\lib\shutil.py", line 127, in copy2
copyfile(src, dst)
File "C:\Python27\lib\shutil.py", line 82, in copyfile
with open(dst, 'wb') as fdst:
IOError: [Errno 22] invalid mode ('wb') or filename: 'D:\\include\\'
Please help me.

I resolved my problem. I wanted copy file to folder witch doesn't exist so i added to code a few lines checking if inputted path exists and if not path is created.

Related

how to elevate python file

Today I was working on a python script that will copy a folder in one directory and paste it in another directory.Everything was going fine but when I ran my code,It gives me an eror masseges like this:
Traceback (most recent call last):
File "c:\Users\sudam\OneDrive\Desktop\programming\python\projects\file_syncer\syncer.py", line 9, in <module>
shutil.copy(pearent_directry , moving_directry)
File "C:\Users\sudam\AppData\Local\Programs\Python\Python310\lib\shutil.py", line 417, in copy
copyfile(src, dst, follow_symlinks=follow_symlinks)
File "C:\Users\sudam\AppData\Local\Programs\Python\Python310\lib\shutil.py", line 254, in copyfile
with open(src, 'rb') as fsrc:
PermissionError: [Errno 13] Permission denied: 'C:/Users/sudam/OneDrive/Documents/Rainmeter'
and here is my code:
print('file syncer version 1.0')
import shutil
from elevate import elevate
elevate()
pearent_directry = 'C:/Users/sudam/OneDrive/Documents/Rainmeter'
moving_directry = 'D:/sync'
shutil.copy(pearent_directry , moving_directry)
print('process has finished with error code:0')
any yes,I did use elevate to try to get admin rights but,It doesn't work either.
Can somebody please help me out in solving this issue?
Bro, it's easier to change the code to fix your problem.
from distutils.dir_util import copy_tree
a = "The path to the file"
b = "The path to the file"
copy_tree(a, b)
print("Process finished")
Also read the PEP8 documentation.

Unexpected "No such file or directory" in Python

My two line code for moving a file into the cwd is as follows:
import os, shutil
shutil.move(os.path.abspath('hello_file.txt'),os.getcwd())
I do have the file just one up the cwd but I get this traceback:
Traceback (most recent call last):
File "<ipython-input-333-f012757a9dca>", line 1, in <module>
shutil.move(os.path.abspath('hello_file.txt'),os.getcwd())
File "/Users/deepayanbhadra/anaconda3/lib/python3.6/shutil.py", line 558, in move
copy_function(src, real_dst)
File "/Users/deepayanbhadra/anaconda3/lib/python3.6/shutil.py", line 257, in copy2
copyfile(src, dst, follow_symlinks=follow_symlinks)
File "/Users/deepayanbhadra/anaconda3/lib/python3.6/shutil.py", line 120, in copyfile
with open(src, 'rb') as fsrc:
FileNotFoundError: [Errno 2] No such file or directory: '/Users/deepayanbhadra/Desktop/DIY-Python-Projects/Automating Tasks/hello_file.txt'
The file path you have provided is the current directory. You first have to change to the directory up. One of the ways you can do this is
import os
os.chdir(“..”)
That’s the simplest way but will take you quite a number of lines of code to achieve the same

Lots of errors regarding shutil, saying a file isn't there when it is in Python

Here's all my code to start with:
import shutil
import glob
import os
for filename in glob.glob(r"C:/Users/Aydan/Desktop/RTHPython/Years/*.txt"):
text_file_name = filename.strip()
dst = (os.path.join(r"C:/Users/Aydan/Desktop/SortedImages", text_file_name))
with open (text_file_name) as my_file:
for filename in my_file:
file_name = filename.strip()
src = (os.path.join('C:/Users/Aydan/Desktop', file_name))
src = src.replace('/', '\\')
shutil.move(src, dst)
I've made this to cycle through each text file in \Years\, and with in each text file, cycle through each line. On each line in the text file is a file directory:
1855.txt contents:
/data01/BL/ER/D11/fmp000005578/BL_ER_D11_fmp000005578_0001_1.txt
/data01/BL/ER/D11/fmp000005578/BL_ER_D11_fmp000005578_0002_1.txt
/data01/BL/ER/D11/fmp000005578/BL_ER_D11_fmp000005578_0003_1.txt
1856.txt contents:
/data01/BL/ER/D11/fmp000005578/BL_ER_D11_fmp000005578_0004_1.txt
/data01/BL/ER/D11/fmp000005578/BL_ER_D11_fmp000005578_0005_1.txt
/data01/BL/ER/D11/fmp000005578/BL_ER_D11_fmp000005578_0006_1.txt
This file directory is used as the source of the file using shutil, and the destination is the name of the text file without .txt, so 1855 and 1856. This means the files that are under 1855 go into the 1855 folder, and the same for 1856.
I'm an continuing to get errors with this code though, but I do not understand why:
Traceback (most recent call last):
File "C:\Users\Aydan\AppData\Local\Programs\Python\Python36-32\lib\shutil.py", line 544, in move
os.rename(src, real_dst)
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'C:/Users/Aydan/Desktop/data01/BL/ER/D11/fmp000005578/BL_ER_D11_fmp000005578_0001_1.txt' -> 'C:\\Users\\Aydan\\Desktop\\RTHPython\\Years\\1855.txt'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#17>", line 8, in <module>
shutil.move(src, dst)
File "C:\Users\Aydan\AppData\Local\Programs\Python\Python36-32\lib\shutil.py", line 558, in move
copy_function(src, real_dst)
File "C:\Users\Aydan\AppData\Local\Programs\Python\Python36-32\lib\shutil.py", line 257, in copy2
copyfile(src, dst, follow_symlinks=follow_symlinks)
File "C:\Users\Aydan\AppData\Local\Programs\Python\Python36-32\lib\shutil.py", line 120, in copyfile
with open(src, 'rb') as fsrc:
FileNotFoundError: [Errno 2] No such file or directory: 'C:/Users/Aydan/Desktop/data01/BL/ER/D11/fmp000005578/BL_ER_D11_fmp000005578_0001_1.txt'
>>>
Even with it saying No such file or directory:, I know it's there as when I paste in the exact directory it has an error on, the text file opens...
I would greatly appreciate help to fix this and come to an almost finish on this project :D
Thanks!
Aydan.
Update code:
import shutil
import glob
import os
for filename in glob.glob(r"C:/Users/Aydan/Desktop/RTHPython/Years/*.txt"):
text_file_name = filename.strip()
dst = (os.path.join(r"C:/Users/Aydan/Desktop/SortedImages", text_file_name))
with open (text_file_name) as my_file:
for filename in my_file:
file_name = filename.strip()
src = (os.path.join('C:/Users/Aydan/Desktop', file_name))
src = src.replace('/', '\\')
shutil.move(src, dst)
It's new errors:
Traceback (most recent call last):
File "C:\Users\Aydan\AppData\Local\Programs\Python\Python36-32\lib\shutil.py", line 544, in move
os.rename(src, real_dst)
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:\\data01\\BL\\ER\\D11\\fmp000005578\\BL_ER_D11_fmp000005578_0001_1.txt' -> 'C:/Users/Aydan/Desktop/RTHPython/Years\\1855.txt'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#11>", line 9, in <module>
shutil.move(src, dst)
File "C:\Users\Aydan\AppData\Local\Programs\Python\Python36-32\lib\shutil.py", line 558, in move
copy_function(src, real_dst)
File "C:\Users\Aydan\AppData\Local\Programs\Python\Python36-32\lib\shutil.py", line 257, in copy2
copyfile(src, dst, follow_symlinks=follow_symlinks)
File "C:\Users\Aydan\AppData\Local\Programs\Python\Python36-32\lib\shutil.py", line 120, in copyfile
with open(src, 'rb') as fsrc:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\data01\\BL\\ER\\D11\\fmp000005578\\BL_ER_D11_fmp000005578_0001_1.txt'
import shutil
import glob
import os
import ntpath
for filename in glob.glob(r"C:/Users/Aydan/Desktop/RTHPython/Years/*.txt"):
text_file_name = filename.strip()
text_name_folder = ntpath.basename(filename)
dst = os.path.join(r"C:/Users/Aydan/Desktop/SortedImages", text_name_folder.strip(".txt"))
dst = dst.replace('/', '\\')
with open (text_file_name) as my_file:
for filename in my_file:
file_name = filename.strip()
src = (os.path.normpath('C:/Users/Aydan/Desktop' + file_name))
src = src.replace('/', '\\')
shutil.move(src, dst)
It works!

Using Pandas to rename files

I'm trying to rename files using an excel sheet and pandas but I keep getting an IO error. In the 'Filename' column I have the original filename along with what I want it to be in the 'rename' column. I know there are other ways to do this but I don't understand why the below doesn't work.
import os
import pandas as pd
from os.path import join
import shutil
dir = os.path.dirname(__file__)
excelFile = join(dir,'test.xlsx')
output_image_dir = os.path.join(dir,'PREAPPROVAL')
df = pd.read_excel(excelFile, sheetname='rename')
for x,y in zip(df['Filename'].astype('str'),df['rename']):
x = join(output_image_dir,x)
y = join(output_image_dir,y)
shutil.move(x,y)
Traceback (most recent call last):
File "D:\Dropbox\1. Projects\2. Python\2. X1\rename_images.py", line 26, in <module>
shutil.move(x,y)
File "c:\python27\lib\shutil.py", line 302, in move
copy2(src, real_dst)
File "c:\python27\lib\shutil.py", line 130, in copy2
copyfile(src, dst)
File "c:\python27\lib\shutil.py", line 82, in copyfile
with open(src, 'rb') as fsrc:
IOError: [Errno 2] No such file or directory: 'D:\\Dropbox\\1. Projects\\2. Python\\2. X1\\PREAPPROVAL\\VERT.jpg'
[Finished in 0.3s]
Sorry for the trouble, but I figured it out. There was an extra space in one of the filenames that threw everything off. Thanks.

Python script to copy files

I am trying to write a Python script that will copy files from one user’s home directory into another user’s home directory. I want it to copy the permissions, as well. I read the Python API and I think that the copy2 method does just that. However, when I run the following code, I get errors.
import shutil
src = raw_input("Please enter a source: ")
dst = raw_input("Please enter a destination: ")
shutil.copy2(src, dst)
The error says:
Traceback (most recent call last):
File "copyfiles.py", line 5, in <module>
shutil.copy2(src, dst)
File "/usr/lib/python2.6/shutil.py", line 99, in copy2
copyfile(src, dst)
File "/usr/lib/python2.6/shutil.py", line 52, in copyfile
fsrc = open(src, 'rb')
IOError: [Errno 2] No such file or directory: '../../../../Desktop/byteswap.c'
Check your current directory using os.getcwd().

Categories