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
Related
I have been using the copy2 function to copy files and it has been working fine on the Linux system with both the source and destination directories in Linux. Recently I tried to use the Windows File share as the backend and was trying to copy files to my linux system. The code and the error displayed is as follows:
for afile in self.task_files:
copy2(os.path.join(self.uploads_directory, afile), self.files_directory)
count += 1`
Output:
Traceback (most recent call last):
File "/opt/backend/worker.py", line 43, in <module>
task.initialize()
File "/opt/backend/services/task.py", line 58, in initialize
copy2(os.path.join(self.uploads_directory, afile), self.files_directory)
File "/usr/lib/python3.5/shutil.py", line 252, in copy2
copystat(src, dst, follow_symlinks=follow_symlinks)
File "/usr/lib/python3.5/shutil.py", line 219, in copystat
_copyxattr(src, dst, follow_symlinks=follow)
File "/usr/lib/python3.5/shutil.py", line 151, in _copyxattr
names = os.listxattr(src, follow_symlinks=follow_symlinks)
OSError: [Errno 38] Function not implemented: '/data/uas/uploads/DJI_0049_vnbh0.JPG'
Is the copy2 function unable to transfer the files because of the OS or is it something else? Also is there any other function in python which can be used to copy files along with its metadata?
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!
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.
Traceback (most recent call last):
File "C:/my_python/read1.py", line 3, in <module>
book = open_workbook('Book1.xls',on_demand=True)
File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 449, in open_workbook
ragged_rows=ragged_rows,
File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 941, in biff2_8_load
f = open(filename, open_mode)
IOError: [Errno 2] No such file or directory: 'Book1.xls'
I am getting this error message in python while trying to open an excel sheet.....How can i solve this ?
Either make sure Book1.xls is in "C:\my_python\", which is also where your script is, or use the full path to where Bool1.xls is located. For example:
book = open_workbook('C:\\Users\\Jeeva\\Documents\\Book1.xls',on_demand=True)
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.