I am trying to find all the downloaded mp4 and mkv files in my downloads folder. The specific files i'm looking for are all in different places in downloads some in subdirectories in the downloads' folder some just files in the downloads' folder.
this is what i have so far
import os
import shutil
os.chdir(r'C:\Users\carte\Downloads')
path = os.path.abspath(r'.\Movie_files')
for p, d, f in os.walk(r'C:\Users\carte\Downloads'):
for file in f:
if file.endswith('.mp4') or file.endswith('.mkv'):
print('-------------------------------------------------------')
print('File Path:' + os.path.abspath(file))
print(f"Movie File:{file}")
print('-------------------------------------------------------')
movie_file_path =os.path.abspath(file)
shutil.move(movie_file_path, path)
but i continue to get this error when runnning
-------------------------------------------------------
File Path:C:\Users\carte\Downloads\1917.mp4
Movie File:1917.mp4
-------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\carte\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: 'C:\\Users\\carte\\Downloads\\1917.mp4' -> 'C:\\Users\\carte\\Downloads\\Movie_files\\1917.mp4'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:/Users/carte/OneDrive/Documents/Code/Practice/practice_os.py", line 13, in <module>
shutil.move(movie_file_path, path)
File "C:\Users\carte\AppData\Local\Programs\Python\Python38-32\lib\shutil.py", line 802, in move
copy_function(src, real_dst)
File "C:\Users\carte\AppData\Local\Programs\Python\Python38-32\lib\shutil.py", line 432, in copy2
copyfile(src, dst, follow_symlinks=follow_symlinks)
File "C:\Users\carte\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: 'C:\\Users\\carte\\Downloads\\1917.mp4'
PS C:\Users\carte\OneDrive\Documents\Code>
Why is the shutil.move looking for the file im trying to move in the destination?
I ran into the same problem when organizing my code it was something to do with the \in my strings I needed to use \\ instead.
I'll paste my code below for what I coded.
It works so you can try to update it for your needs.
from shutil import move as mve
from time import sleep as slp
import os
os.chdir('c:\\Users\\Charlie\\Downloads')
source_path = "c:\\Users\\Charlie\\Downloads"
dest_path = "c:\\users\\Charlie\\Downloads\\FORGOT TO USE A PATH"
def move_file():
# get the current date
src_path = os.path.join(source_path, f)
# create the folders if they arent already exists
if not os.path.exists(dest_path):
os.makedirs(dest_path)
if not os.path.exists(f"{dest_path}\\{f}"):
mve(src_path, dest_path)
else:
print("File already exists")
while True:
files = os.listdir("c:\\Users\\Charlie\\Downloads")
for f in files:
if f.endswith('.zip'):
dest_path = "c:\\users\\Charlie\\Downloads\\Zip"
move_file()
elif f.endswith('.pdf') or f.endswith('.docx') or f.endswith('.doc') or f.endswith('.ppt') or f.endswith('.pptx'):
dest_path = "c:\\users\\Charlie\\Downloads\\Documents"
move_file()
elif f.endswith('.jpg') or f.endswith('.png'):
dest_path = "c:\\users\\Charlie\\Downloads\\Pictures"
move_file()
elif f.endswith('.tmp'):
break
elif not os.path.isdir(f):
dest_path = "c:\\users\\Charlie\\Downloads\\Unsorted"
move_file()
slp(10)
Check if the destination directory exists.
Also, it may be a problem that your destination is actually part of your source tree, so you may want to put the files elsewhere.
Related
I'm trying to use shutil.copystat() to copy a file. This is my code:
import os
from os import path
import shutil
def main():
if path.exists("textfile.txt"):
src = path.realpath("textfile.txt")
dest = src + ".bak"
shutil.copystat(src, dest)
if __name__ == "__main__":
main()
However, this results in the following error:
Traceback (most recent call last):
File "/Users/x/Development/Python/Exercise Files/Ch4/shell_start.py", line 30, in <module>
main()
File "/Users/x/Development/Python/Exercise Files/Ch4/shell_start.py", line 19, in main
shutil.copystat(src, dest)
File "/opt/homebrew/Cellar/python#3.9/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/shutil.py", line 375, in copystat
lookup("utime")(dst, ns=(st.st_atime_ns, st.st_mtime_ns),
FileNotFoundError: [Errno 2] No such file or directory
I have printed the paths and both are correct. My file structure is as follows:
/Ch4
/shell_start.py
/textfile.txt
Does anyone know what I'm doing wrong here?
Replace:
shutil.copystat(src, dest)
by:
shutil.copy2(src, dest)
If you want to use shutil.copystat, do:
shutil.copy(src, dest)
shutil.copystat(src, dest)
Read the doc of shutil.copy2
shutil.copystat doesn't copy files, it only copies the status bits between two existing files. What you want is shutil.copy2.
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)
Based on the first 3 characters of a file name i want to create a folder then copy in the related file.
I have a script that works the first time, however i get an error if I run it multiple times
I believe i need to check if the file exists first, however i haven't been able to get it work.
Or to filter out the newly created folders from the os.list
Any help would be greatly appreciated:
srcpath = 'C:\\temp\\Test'
srcfiles = os.listdir(srcpath)
destpath = 'C:\\temp\\Test'
# extract the three letters from filenames
destdirs = list(set([filename[0:3] for filename in srcfiles]))
def create(destdirs, destpath):
full_path = os.path.join(destpath, destdirs)
if not os.path.exists(full_path):
os.mkdir(full_path)
return full_path
def copy(filename, dirpath):
shutil.copy(os.path.join(srcpath, filename), dirpath)
# create destination directories and store their names along with full paths
targets = [
(folder, create(folder, destpath)) for folder in destdirs
]
for destdirs, full_path in targets:
for filename in srcfiles:
if destdirs == filename[0:3]:
copy(filename, full_path)
ERROR
Traceback (most recent call last):
File "C:/Users/Desktop/copy_only.py", line 45, in <module>
copy(filename, full_path)
File "C:/Users/Desktop/copy_only.py", line 35, in copy
shutil.copy(os.path.join(srcpath, filename), dirpath)
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: 'C:\\temp\\Test\\F12'
It looks like you are trying to open a directory C:\\temp\\Test\\F12 as the filename in copy.
Otherwise, please check if you have the permission to open/read the file.
I am having trouble moving files from one folder to another. I have written this simple code:
import os
import shutil
movePath = "C:\\Users\\BWhitehouse\\Documents\\GVRD\\MonthlySummary_03\\SCADA"
destPath = "I:\\eng\\GVRD\\Rain_Gauges\\MonthlyDownloads\\2014-03"
for dirpath, dirs, files in os.walk(movePath):
for file in files:
if file.endswith('.xls'):
shutil.copy(file, destPath)
And this is the error I am getting:
Traceback (most recent call last):
File "C:\Python34\test.py", line 12, in <module> shutil.copy(file, destPath)
File "C:\Python34\lib\shutil.py", line 228, in copy copyfile(src, dst, follow_symlinks=follow_symlinks)
File "C:\Python34\lib\shutil.py", line 107, in copyfile with open(src, 'rb') as fsrc:
FileNotFoundError: [Errno 2] No such file or directory: 'BU07-201403.xls'
If anyone could help me out that would be greatly appreciated!
The file variable is just the name, to get the full path add it to the dirpath variable:
shutil.copy( os.path.join(dirpath, file), destPath )
Do you have full access to these folders? First check it out.
Start Python as Administrator by right-clicking, when you try to start the script.
I had the same problem. I solved the problem in this way.