I am trying to write a program that sorts my downloads; however, when I try to run it I get this:
Traceback (most recent call last):
File "/usr/lib/python3.5/shutil.py", line 538, in move
os.rename(src, real_dst)
FileNotFoundError: [Errno 2] No such file or directory:'UntitledDocument.txt' -> 'Downloads/TxtFiles/UntitledDocument.txt'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/nicholas/Python/DownloadSorter.py", line 19, in <module>
shutil.move(file, folders[fileType])
File "/usr/lib/python3.5/shutil.py", line 552, in move
copy_function(src, real_dst)
File "/usr/lib/python3.5/shutil.py", line 251, in copy2
copyfile(src, dst, follow_symlinks=follow_symlinks)
File "/usr/lib/python3.5/shutil.py", line 114, in copyfile
with open(src, 'rb') as fsrc:
FileNotFoundError: [Errno 2] No such file or directory: 'UntitledDocument.txt'
This is the code :
#!/usr/bin/python3
import os
import shutil
folders = {
'.tar.gz': 'Downloads/TarFiles',
'.deb': 'Downloads/DebFiles',
'.iso': 'Downloads/IsoFiles',
'.txt': 'Downloads/TxtFiles',
'.exe': 'Downloads/ExeFiles',
'.mp3': 'Videos',
'.wav': 'Music'
}
os.chdir('/home/nicholas/')
for file in os.listdir('Downloads'):
for fileType in folders.keys():
if file.endswith(fileType):
shutil.move(file, folders[fileType])
The problem is that you are using the filename without the path and shutil looks for the file in the working directory intead of Downloads directory.
The fix is simple:
shutil.move(os.path.join('Downloads', file), folders[fileType])
Related
This is the code, it searches for through screenshots and takes those that have a specific string.
import os
import shutil
#from pathlib import Path
#'E:\\Test'
#if src = 'path/to/file.txt'
#grabs = 'C:\\Users\\Gal\\Videos\\Captures'
#'C:\\Users\\Gal\\Videos\\Captures'
directory = ('C:\\Users\\Gal\\Videos\\Captures')
target = ('C:\\zorko')
destination = 'zorko'
for filename in os.listdir(directory):
data = str(filename)
final = 'teorija' in data
path = os.path.abspath(filename)
if final is True:
print('1')
shutil.move(filename, target)
Howevere when trying to move them with shutil it displays this error:
Traceback (most recent call last):
File "C:\Users\Gal\AppData\Local\Programs\Python\Python38-32\lib\shutil.py", line 788, in move
os.rename(src, real_dst)
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'T1A INF teorija _ Microsoft Teams 10_21_2020 10_39_27 AM.png' -> 'C:\\zorko\\T1A INF teorija _ Microsoft Teams 10_21_2020 10_39_27 AM.png'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/Gal/PycharmProjects/pythonProject2/main.py", line 23, in <module>
shutil.move(filename, target)
File "C:\Users\Gal\AppData\Local\Programs\Python\Python38-32\lib\shutil.py", line 802, in move
copy_function(src, real_dst)
File "C:\Users\Gal\AppData\Local\Programs\Python\Python38-32\lib\shutil.py", line 432, in copy2
copyfile(src, dst, follow_symlinks=follow_symlinks)
File "C:\Users\Gal\AppData\Local\Programs\Python\Python38-32\lib\shutil.py", line 261, in copyfile
with open(src, 'rb') as fsrc, open(dst, 'wb') as fdst:
FileNotFoundError: [Errno 2] No such file or directory: 'T1A INF teorija _ Microsoft Teams 10_21_2020 10_39_27 AM.png'
import os
import shutil
directory = ('C:\\Users\\Gal\\Videos\\Captures')
target = ('C:\\zorko')
destination = 'zorko'
for filename in os.listdir(directory):
if 'teorija' in filename:
shutil.move(os.path.join(directory, filename), os.path.join(target, filename))
I am trying to make a script that gets all the pictures of my Images folder that have the jpg extension and moves them in the newimages folder.
Here is my code:
import os
import shutil
for filename in os.listdir("D:/Images/"):
if filename.endswith(".jpg"):
shutil.move(filename, r'D:/newimages/')
However when I run the code, I get the following error:
Traceback (most recent call last):
File "d:\Online_courses\Coursera\Google_IT_Automation\Automating_real-world_tasks_with_python\project1\script1.py", line 9, in <module>
shutil.move(filename, r'D:/newimages/')
File "C:\Users\Nicholas\AppData\Local\Programs\Python\Python37\lib\shutil.py", line 580, in move
copy_function(src, real_dst)
File "C:\Users\Nicholas\AppData\Local\Programs\Python\Python37\lib\shutil.py", line 266, in copy2
copyfile(src, dst, follow_symlinks=follow_symlinks)
File "C:\Users\Nicholas\AppData\Local\Programs\Python\Python37\lib\shutil.py", line 120, in copyfile
with open(src, 'rb') as fsrc:
FileNotFoundError: [Errno 2] No such file or directory: '20180331_164750.jpg'
when you use a filename it will just take a name. For copying you need a full path. Please try the same but with
for filename in os.listdir("D:/Images/"):
if filename.endswith(".jpg"):
shutil.move(os.path.join("D:/Images/", filename), os.path.join("D:/NewImages/", filename))
You need to append 'D:/Images/' to each filename. You're not in the D:/Images/ directory so Python isn't able to find those files.
old_dir = 'D:/Images'
new_dir = 'D:/newimages'
for filename in os.listdir(old_dir):
if filename.endswith(".jpg"):
shutil.move(f'{old_dir}/{filename}', new_dir)
I have a function where I am trying to copy an XML document to my Dropbox folder, but I am getting:
/usr/bin/python3.5 /home/cali/PycharmProjects/Vocabulary/Vocabulary.py
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.5/tkinter/__init__.py", line 1553, in __call__
return self.func(*args)
File "/home/cali/PycharmProjects/Vocabulary/Vocabulary.py", line 232, in add_item
self.sync()
File "/home/cali/PycharmProjects/Vocabulary/Vocabulary.py", line 287, in sync
copyfile(vocabulary_path, destination_path)
File "/usr/lib/python3.5/shutil.py", line 115, in copyfile
with open(dst, 'wb') as fdst:
IsADirectoryError: [Errno 21] Is a directory: '/home/cali/Dropbox/'
Process finished with exit code 0
Here is the function:
def sync(self):
path = os.path.expanduser('~/Desktop')
vocabulary_path = os.path.join(path, 'Vocabulary', 'Words.xml')
destination_path = os.path.expanduser('~/Dropbox/')
copyfile(vocabulary_path, destination_path)
How can I overcome the issue?
Perhaps by copying the file to another file, rather than a directory.
...
destination_path = os.path.expanduser('~/Dropbox/Words.xml')
...
I wrote this program to move videos from my download folder to different destination folders.
import os
import shutil
import sys
folder = []
highlight = ['highlights','goals','skills']
comedy_word = ['comedy', 'acapella','seyilaw','basketmouth','basket mouth','bovi','gordons','buchi','rhythm unplugged','elenu','seyi law','akpororo','emmaohmygod','klint','shakara','funnybone','igodye','i go die','i go dye','igodye','whalemouth','whale mouth','daniel']
series_word = ['lost', 'thrones', 'vampire', 'originals', 'ship', '']
grub_word = ['programming', 'python', 'linux','exploit','hack']
for i in os.listdir('C:\\Users\\Afro\\Downloads\\Video'):
folder.append(i.lower())
def tv_series(series):
for serie in series:
if 'ship' in serie:
shutil.move(serie, 'C:\\Users\\Afro\\Desktop\\Movies\\Series\\The Last Ship\\The Last Ship 3\\' )
print(serie[:-3] +': Moved!')
elif 'lost' in serie:
shutil.move(serie, 'C:\\Users\\Afro\\Desktop\\Movies\\Series\\Lost\\s2\\' )
print(serie[:-3] +': Moved!')
The file's name is arrange.py and the program throws a filenotfound error when arrange.py is not in the "C:\Users\Afro\Downloads\Video" folder. Is it possible to make the program run in any folder?
Thanks
Here's the error it throws.
Traceback (most recent call last):
File "C:\Users\Afro\AppData\Local\Programs\Python\Python35\lib\shutil.py", line 538, in move
os.rename(src, real_dst)
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'lost - s02e08 (o2tvseries.com).mp4' -> 'C:\\Users\\Afro\\Desktop\\Movies\\Series\\Lost\\s2\\lost - s02e08 (o2tvseries.com).mp4'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Afro\Desktop\Arrange.py", line 77, in <module>
start(folder)
File "C:\Users\Afro\Desktop\Arrange.py", line 65, in start
tv_series(folder)
File "C:\Users\Afro\Desktop\Arrange.py", line 22, in tv_series
shutil.move(serie, 'C:\\Users\\Afro\\Desktop\\Movies\\Series\\Lost\\s2\\' )
File "C:\Users\Afro\AppData\Local\Programs\Python\Python35\lib\shutil.py", line 552, in move
copy_function(src, real_dst)
File "C:\Users\Afro\AppData\Local\Programs\Python\Python35\lib\shutil.py", line 251, in copy2
copyfile(src, dst, follow_symlinks=follow_symlinks)
File "C:\Users\Afro\AppData\Local\Programs\Python\Python35\lib\shutil.py", line 114, in copyfile
with open(src, 'rb') as fsrc:
FileNotFoundError: [Errno 2] No such file or directory: 'lost - s02e08 (o2tvseries.com).mp4'
You call shutil.move specifying the reference point incorrectly. In your loop that gathers the files, you should use os.path.join('C:\\Users\\Afro\\Downloads\\Video', i) and put that in your list instead. Otherwise it's all relative paths, hence the FileNotFound error.
You could also change the working directory. This would make the script behave as if it was in a different folder. This can have some unintended consequences, so be careful. Details here.
Hope this helps!
i have a problem with combining 2 folder with it subdirectories, i have 2 folder source. i named it : test-2 and test-2 and path to these folder is output/test-2 and output/test-4
i combined it with this line:
merged_folder_path = 'merged/dir/output'
shutil.copy(result_1_path, merged_folder_path)
shutil.copy(result_2_path, merged_folder_path)
but it get error:
Traceback (most recent call last):
File "jamu.py", line 769, in <module>
main(sys.argv)
File "jamu.py", line 757, in main
shutil.copy(result_1_path, merged_folder_path)
File "C:\Python27\lib\shutil.py", line 119, in copy
copyfile(src, dst)
File "C:\Python27\lib\shutil.py", line 82, in copyfile
with open(src, 'rb') as fsrc:
IOError: [Errno 13] Permission denied: 'output/test-2'
is something wrong with my code?
i get the same problem here.
and it's working to me,
thanks for all the answers. apreciated it!
Check perms before copy/move/delete
import os
if os.access(merged_folder_path,os.W_OK):
shutil.copy(...,...)
os.F_OK: path is exists
os.R_OK: readability of path.
os.W_OK: writability of path.
os.X_OK path can be executed.