I have a folder with a few files that I would like to copy one directory up (this folder also has some files that I don't want to copy). I know there is the os.chdir("..") command to move me to the directory. However, I'm not sure how to copy those files I need into this directory. Any help would be greatly appreciated.
UPDATE:
This is what I have now:
from shutil import copytree, ignore_patterns
copytree("/Users/aaron/Desktop/test/", "/Users/aaron/Desktop/", ignore=ignore_patterns('*.py', '*.txt'))
I am getting the following error:
Traceback (most recent call last):
File "update.py", line 61, in <module>
copytree("/Users/aaron/Desktop/test/", "/Users/aaron/Desktop/", ignore=ignore_patterns('*.py', '*.txt'))
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/shutil.py", line 146, in copytree
os.makedirs(dst)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/os.py", line 157, in makedirs
mkdir(name, mode)
OSError: [Errno 17] File exists: '/Users/aaron/Desktop/'
The shutil module can do this, specifically the copyfile, copy, copy2 and copytree functions. http://docs.python.org/library/shutil.html
You probably want something along these lines:
import os
import shutil
fileList = os.listdir('path/to/source_dir')
fileList = ['path/to/source_dir/'+filename for filename in fileList]
for f in fileList:
shutil.copy2(f, 'path/to/dest_dir/')
You can of course filter some file names out during the call to os.listdir(). For example,
fileList = [filename for filename in os.listdir('path/to/source_dir') if filename[-3] is '.txt']
instead of fileList = os.listdir('path/to/source_dir') to get just the .txt files
Related
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 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 have a simple code to zip files using zipfile module. I am able to zip some files but I get FileNotFound error for the others. I have checked if this is file size error but its not.
I can pack files with name like example file.py but when I have a file inside a directory like 'Analyze Files en-US_es-ES.xlsx' if fails.
It works when I change os.path.basename to os.path.join but I don't want to zip whole folder structure, I want to have flat structure in my zip.
Here is my code:
import os
import zipfile
path = input()
x=zipfile.ZipFile('new.zip', 'w')
for root, dir, files in os.walk(path):
for eachFile in files:
x.write(os.path.basename(eachFile))
x.close()
Error looks like this:
Traceback (most recent call last):
File "C:/Users/mypc/Desktop/Zip test.py", line 15, in <module>
x.write(os.path.basename(eachFile))
File "C:\Python34\lib\zipfile.py", line 1326, in write
st = os.stat(filename)
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'Analyze Files en-US_ar-SA.xlsx'*
Simply change working directory to add file without original directory structure.
import os
import zipfile
path = input()
baseDir = os.getcwd()
with zipfile.ZipFile('new.zip', 'w') as z:
for root, dir, files in os.walk(path):
os.chdir(root)
for eachFile in files:
z.write(eachFile)
os.chdir(baseDir)
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.