I wrote this function to achieve a similar behavior as in Windows when moving files and directories. In particular, objects should be overwritten.
from pathlib import Path
import shutil
import os
def move_anyhow(source: Path | str, dest: Path | str) -> Path:
"""
Move source (directory or file) and overwrite files with same name in dest if exists.
"""
try:
shutil.move(source, dest)
except shutil.Error:
if source.is_file():
shutil.move(source, dest / source.name)
else:
for path in source.iterdir():
move_anyhow(path, dest / source.name)
os.rmdir(source)
return dest / source.name
I took a recursive approach to moving nested source directories like this one
.../source/
dir_A/
dir_B/
file_X
to destination
.../dest/
dir_A/
dir_B/
file_X
file_Y
On production I get a PermissionError now and then which looks like this:
PermissionError: [Errno 13] Permission denied: '/delivery/post/01_FROM_CF/W22_FW/50479944_003' -> '/delivery/post/01_FROM_CF/ERROR/W22_FW/50479944_003'
File "shutil.py", line 813, in move
os.rename(src, real_dst)
OSError: [Errno 39] Directory not empty: '/delivery/post/01_FROM_CF/W22_FW/50479944_003'
File "ors/path.py", line 34, in move_anyhow
shutil.move(source, dest)
File "shutil.py", line 831, in move
rmtree(src)
File "shutil.py", line 728, in rmtree
onerror(os.rmdir, path, sys.exc_info())
File "shutil.py", line 726, in rmtree
os.rmdir(path)
All files were moved but the empty source folder remained. I can't reproduce this error locally. So I my first guess was that this is a server issue. Still, I wonder if the nested approach could cause this error.
So I guess my question is whether an catched shutil.move error can block another shutil.move operation of a file inside the source directory.
The problem will probably be in the timing of files deletion and directory deletion. I would try to cancel the immediate deletion of directories and I would delete the directories only at the end.
Remove the command line:
os.rmdir(source)
and when the file transfer is complete, then call the command
shutil.rmtree(source)
Good luck.
Related
So I have a really big folder from a dataset downloaded online that has almost 5500 folder each containing 6 or so images. Some of those images have cloth_front_mask in them.
Since there are way to many folder for me to go through each one of them and try to move the files to another directory, I tried creating a small script that would do that task for me but when I tried it I get a
Traceback (most recent call last):
File "C:\Users\Adam\AppData\Local\Programs\Python\Python37-32\lib\shutil.py", line 557, in move
os.rename(src, real_dst)
FileNotFoundError: [WinError 2] The system cannot find the file specified: '10K21D003-C11#2=cloth_front_mask.jpg' -> 'C:\\Users\\Adam\\Downloads\\LIP_MPV_256_192\\MPV_256_192\\all\\all\\images\\cloth_mask\\10K21D003\\10K21D003-C11#2=cloth_front_mask.jpg'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#52>", line 9, in <module>
shutil.move(name, path, copy_function=shutil.copytree)
File "C:\Users\Adam\AppData\Local\Programs\Python\Python37-32\lib\shutil.py", line 571, in move
copy_function(src, real_dst)
File "C:\Users\Adam\AppData\Local\Programs\Python\Python37-32\lib\shutil.py", line 309, in copytree
names = os.listdir(src)
FileNotFoundError: [WinError 3] The system cannot find the path specified: '10K21D003-C11#2=cloth_front_mask.jpg'
Here's my code:
for root, dirs, files in os.walk('.'):
for name in files:
if 'mask' in name:
path = os.path.join('C:\\Users\\Adam\\Downloads\\LIP_MPV_256_192\\MPV_256_192\\all\\all\\images\\cloth_mask', name[:9])
if os.path.exists(path):
pass
else:
os.mkdir(path)
shutil.move(name, path, copy_function=shutil.copytree)
I got it. The problem was that in the shutil.move line I didn't specify the file's original folder. When I did so, it worked fine.
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.
I'm trying to make a script that crawls through the Applications directory and open up a given file. So here is my code
import os, subprocess
os.chdir('/Applications')
root = '.'
for path, dirs, files in os.walk(root):
#print path
for f in files:
if f == 'Atom':
subprocess.call([f])
break
So I have three questions.
At first I used Atom as the example to execute the script. It opens up fine, but even after opening the app the loop doesn't break and keeps crawling.
Second, the Atom app doesn't open up as it would normally do. It opens up in the directory of the applications folder, which looks something like this.
While it should merely look like this,
And the very important problem is that it didn't work for any other applications which I couldn't understand. Here is the error output when I tried to open AppStore.
./App Store.app
./App Store.app/Contents
./App Store.app/Contents/_CodeSignature
./App Store.app/Contents/MacOS
Traceback (most recent call last):
File "control_files.py", line 32, in <module>
subprocess.call([f])
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 522, in call
return Popen(*popenargs, **kwargs).wait()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 709, in __init__
errread, errwrite)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1326, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
What could possibly be the problem?
The previous answers are spot-on about break only exiting the innermost loop.
Another way to escape the loop, and probably neater, would be to hide away this functionality in a function and return from it. Something along the lines of:
def open_program(root, filename):
for path, dirs, files in os.walk(root):
if filename in files:
full_path = os.path.join(path, filename)
subprocess.call([full_path])
return
IMO using filename in files makes the code cleaner, and does pretty much the same work.
I looked for similar issues in this forum, but not came up with my situation.
I program python with Eclipse. I am using shutil.move to move some files from one directory to another. When I run the code first, it is giving me the following error. Right after this I run a second try (without changing anything), it finds me the correct files and move to the correct place. Does anyone know what I am doing wrong? If my code is faulty, why it is running in second try without any problem? The source and destination directories are already existing.
Here is the IOError:
Traceback (most recent call last):
File "C:\Users\john\workspace\RC\src\Test.py", line 82, in <module>
shutil.move('C:/RCTemp/' + filename, dest_sonst)
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: 'C:/RCTemp/Details.xslm'
And this is my code:
import os
import shutil
dest_dkfrontend = 'C:/RhodeCode/11_Detailkonzept_Frontend/'
for filename in source:
if filename.startswith('Details'):
print('Files found ' + filename)
shutil.move("C:/RhodeCodeTemp/" + filename, dest_dkfrontend)
Can anyone help please?
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.