Copy Files From List Python - python

Im making a small python program to copy some files. My filenames are in a list "selectedList".
The user has selected the source dir "self.DirFilename" and the destination dir "self.DirDest".
I'm using cp instead of shutil because I've read that shutil is slow.
Heres my code:
for i in selectedList:
src_dir = self.DirFilename + "/" + str(i) + ".mov"
dst_dir = self.DirDest
r = os.system('cp -fr %s %s' % (src_dir, dst_dir))
if r != 0:
print 'An error occurred!'**
I would like the copy to search the source directory for the given filename and then recreate the folder structure in the destination as well as copy the file.
Any suggestions would be helpful (like any massively obvious mistakes that i'm making)- its my first python programme and I'm nearly there!
Thanks
Gavin

I think something like this could do the trick. Of course you may want to use something ore advance that os.system to call cp.
import os
for r, d, f in os.walk(self.DirFilename):
for file in f:
f_name, f_ext = os.path.splitext(file)
if ".mov" == f_ext:
if f_name in selectedList:
src_abs_path = os.path.join(r, file)
src_relative_path = os.path.relpath(src_abs_path, self.DirFilename)
dst_abs_path = os.path.join(self.DirDest, src_relative_path)
dst_dir = os.path.dirname(dst_abs_path)
if not os.path.exists(dst_dir):
os.makedirs(dst_dir)
ret = os.system('cp -fr %s %s' % (src_abs_path, dst_abs_path))
if ret != 0:
print 'An error occurred!'

See http://blogs.blumetech.com/blumetechs-tech-blog/2011/05/faster-python-file-copy.html for a pure Python implementation of the recursive copy.
You can use os.walk to find the file you need:
def find_files(...):
for ... in os.walk(...):
if ...:
yield filename
for name in find_files(...):
copy(name, ...)

import glob
for fname in selectedList:
filename = str(fname) + '.mov'
found = glob.glob(os.path.join(self.DirFilename, filename))
found.extend(glob.glob(os.path.join(self.DirFilename, '**', filename)))
found = [(p, os.path.join(self.DirDest, os.path.relpath(p, self.DirFilename))) for p in found]
for found_file in found:
# copy files however
#r = os.system('cp -fr %s %s' % found_file)

Related

How to sort a list by creation date? (Python - OS library)

I'd like to rename a list of video cuts in a folder (in my case /Desktop/Cuts/)
from:
xxx.mp4, yyy.mp4, ..., zzz.mp4
to:
1.mp4, 2.mp4, ..., 33.mp4.
1.mp4 will be the video that was first created in the folder. 2.mp4 will be the second, etc.
What I am doing now (please see code below) renames the video cuts in /Cuts/, but according to an order that I don't understand, and that is definitely NOT creation date. (it does not seem alphabetical, either)
import os
path = '/Users/ntelmaut/Desktop/Cuts/'
i = 1
for file in os.listdir(path):
new_file_name = "{}.mp4".format(i)
os.rename(path + file, path + new_file_name)
#print path + file
i = i+1
I have tried make a variable out of os.listdir(path) and to apply sorted() function, but to no avail yet (I am a newbie to Python - sorry).
Would love a pointer, here.
Thank you!
Solution
import glob
import os
dirpath = '/Users/ntelmaut/Desktop/Cuts/'
files = glob.glob(os.path.join(path, '*.mp4'))
files.sort(key=os.path.getmtime)
for i, filename in enumerate(files, 1):
try:
os.rename(filename, "{0}.mp4".format(i))
except OSError as e:
print("OSError: ", e)
If I understand correctly this is what you want.
import os
path = '/Users/ntelmaut/Desktop/Cuts/'
def sort_key(file):
return os.stat(file).st_ctime
files = os.listdir(path)
sorted_files = sorted((os.path.join(path, f) for f in files), key=sort_key)
i = 1
for file in sorted_files:
new_file_name = f"{i}.mp4"
os.rename(file, new_file_name)
i += 1
A cleaner way would be to use the pathlib module.
from pathlib import Path
p = Path('/Users/ntelmaut/Desktop/Cuts/')
def sort_key(path):
return path.stat().st_ctime
sorted_files = sorted(p.iterdir(), key=sort_key)
for idx, file in enumerate(sorted_files, 1):
new_name = f"{idx}.mp4"
file.rename(p / new_name)

Get all files from my C drive - Python

Here is what I try to do:
I would like to get a list of all files that are heavier than 35 MB in my C drive.
Here is my code:
def getAllFileFromDirectory(directory, temp):
files = os.listdir(directory)
for file in files:
if (os.path.isdir(file)):
getAllFileFromDirectory(file, temp)
elif (os.path.isfile(file) and os.path.getsize(file) > 35000000):
temp.write(os.path.abspath(file))
def getFilesOutOfTheLimit():
basePath = "C:/"
tempFile = open('temp.txt', 'w')
getAllFileFromDirectory(basePath, tempFile)
tempFile.close()
print("Get all files ... Done !")
For some reason, the interpreter doesn't go in the if-block inside 'getAllFileFromDirectory'.
Can someone tell me what I'm doing wrong and why (learning is my aim). How to fix it ?
Thanks a lot for your comments.
I fixed your code. Your problem was that os.path.isdir can only know if something is a directory if it receives the full path of it. So, I changed the code to the following and it works. Same thing for os.path.getsize and os.path.isfile.
import os
def getAllFileFromDirectory(directory, temp):
files = os.listdir(directory)
for file in files:
if (os.path.isdir(directory + file)):
if file[0] == '.': continue # i added this because i'm on a UNIX system
print(directory + file)
getAllFileFromDirectory(directory + file, temp)
elif (os.path.isfile(directory + file) and os.path.getsize(directory + file) > 35000000):
temp.write(os.path.abspath(file))
def getFilesOutOfTheLimit():
basePath = "/"
tempFile = open('temp.txt', 'w')
getAllFileFromDirectory(basePath, tempFile)
tempFile.close()
print("Get all files ... Done !")
getFilesOutOfTheLimit()

python create recursive folder structure

I'm writing a script to automate the creation of a test file/folder structure - input for another script (to move only some files according to a file list). My code below works, but is there a more pythonic way to complete the same task?
import os
import shutil
os.chdir('c:/')
if not os.path.exists('c:/pythontest'):
os.mkdir('c:/pythontest')
else:
shutil.rmtree('c:/pythontest')
os.mkdir('c:\pythontest')
os.chdir('c:/pythontest')
for i in range(0,3):
os.mkdir('folder%d' % i)
fileName = 'folder%d' % i
filePath = os.path.join(os.curdir, fileName)
print filePath
os.chdir(filePath)
for j in range(0,3):
os.mkdir('folder%d_%d' % (i,j))
fileName = 'folder%d_%d' % (i,j)
filePath = os.path.join(os.curdir, fileName)
print str(filePath)
os.chdir(filePath)
for k in range(0,3):
try:
f = open('file%d_%d_%d.txt' % (i,j,k), 'w')
except IOError:
pass
os.chdir('..')
os.chdir('..')
I can only suggest several minor style improvements -- and moving everything within a function, which speeds things up. E.g:
import os
import shutil
def doit():
shutil.rmtree('c:/pythontest', ignore_errors=True)
os.mkdir('c:/pythontest')
os.chdir('c:/pythontest')
for i in range(0,3):
fileName = 'folder%d' % i
print fileName
os.mkdir(fileName)
os.chdir(fileName)
for j in range(0,3):
fileName = 'folder%d_%d' % (i,j)
print fileName
os.mkdir(fileName)
os.chdir(fileName)
for k in range(0,3):
try:
with open('file%d_%d_%d.txt' % (i,j,k), 'w'):
pass
except IOError:
pass
os.chdir('..')
os.chdir('..')
The minor but cumulative improvements include avoidance of repetition, and avoidance of redundancy (such as prepending a './' to filenames to make exactly equivalent filepaths).

Error on deleting file in a python code

I have a little code that allows me to print images arriving in a folder.
But I'd like them to be deleted just after having been printed - I want to try this on a RaspberryPI and I will not have enough space to store all images.
If someone can help me it would be very much appreciated.
Here is my code:
monRep = "/Users/XX/Desktop/XXX/"
import os, mimetypes, random
while True:
fpaths = []
for fname in os.listdir(monRep):
fpath = os.path.join(monRep, fname)
if os.path.isfile(fpath):
mt = mimetypes.guess_type(fpath)[0]
ext = os.path.splitext(fpath)[1]
if mt: mt = mt.split('/')[0].lower()
else: mt = False
#if ext.lower() in ('.bmp','.pict', '.JPG', '.jpg', '.pdf'): mt = 'image'
if mt in ('image',): fpaths.append(fpath)
for fpath in fpaths:
newpath = fpath.replace('/Users/XX/Desktop/XXX/','/Users/XX/Desktop/XXX2/')
os.rename(fpath,newpath)
command = "lpr "+newpath
print (command)
os.system(command)
I tried to write at the end
os.remove ('/Users/Aym/Desktop/eden2/')
But then I had this :
SError: [Errno 1] Operation not permitted: '/Users/XX/Desktop/XXX2/'
I tried the shutil method recommanded on this forum
import os
import shutil
for root, dirs, files in os.walk('/Users/XX/Desktop/XXX2/'):
for f in files:
os.unlink(os.path.join(root, f))
for d in dirs:
shutil.rmtree(os.path.join(root, d))
but nothing happened
I have also less space on a Raspberry. What you can exchange first of all is:
LIST_NAME = ['.jpg', '.bmp', '.mickey_mouse']
file = [file for file in os.listdir(CAMERA_DIR) for type_name in LIST_NAME if file.endswith(type_name)]
That is somehow equivalent to your whole search engine above.
where LIST_NAME is whatever you look for and CAMERA_DIR == "/Users/XX/Desktop/XXX/"
The lines above will make your code easier and compact without usage of extra modules.
You can use a simple move action from XX to XX2 (shutil module) for each found file under os.path.join(CAMERA_DIR, file_name).
Last step after you print or do whatever, remove completely the XX2 folder with simple command:
shutil.rmtree(XX2)
This is definitely work and clean up your code in Python3. Have fun!
What I can even recommend is: usage of /tmp/NAMEXX folder. In worst case it is only on the temporary folder of your raspberry!

Python: Replace one list with another list?

I'm stuck. I'm moving folders around on our network which all have a unique ID into a central location. There are a few folders with typos and therefore do not match a unique ID in the central location. I have found the correct IDS but I need to rename these folders before I move them. For example, I have created an excel spreadsheet with the wrong unique ID and in a separate column have the correct ID. Now, I want to rename the folders with the correct ID and then transfer those folders to the central location. My code is....rough because I can't think of a good way to do it. I feel like using a list is the way to go, but since my code is iterating through a folder I'm not sure how to achieve this
Edit: I think something like this may be what I'm looking for
Ex:
In Folder A : A file named 12334 SHOULD be renamed 1234. Then moved to the base directory with in folder 1234.
Heres my code:
import os
import re
import sys
import traceback
import collections
import shutil
movdir = r"C:\Scans"
basedir = r"C:\Links"
subfolder = "\Private Drain Connections"
try:
#Walk through all files in the directory that contains the files to copy
for root, dirs, files in os.walk(movdir):
for filename in files:
#find the name location and name of files
path = os.path.join(root, filename)
#file name and extension
ARN, extension = os.path.splitext(filename)
print ARN
#Location of the corresponding folder in the new directory
link = os.path.join(basedir, ARN)
if not os.path.exists(link):
newname = re.sub(372911000002001,372911000003100,ARN)
newname =re.sub(372809000001400,372909000001400,ARN)
newname =re.sub(372809000001500,372909000001500,ARN)
newname =re.sub(372809000001700,372909000001700,ARN)
newname = re.sub(372812000006800,372912000006800,ARN)
newname =re.sub(372812000006900,372912000006900,ARN)
newname =re.sub(372812000007000,372912000007000,ARN)
newname =re.sub(372812000007100,372912000007100,ARN)
newname =re.sub(372812000007200,372912000007200,ARN)
newname =re.sub(372812000007300,372912000007300,ARN)
newname =re.sub(372812000007400,372912000007400,ARN)
newname =re.sub(372812000007500,372912000007500,ARN)
newname =re.sub(372812000007600,372912000007600,ARN)
newname =re.sub(372812000007700,372912000007700,ARN)
newname =re.sub(372812000011100,372912000011100,ARN)
os.rename(os.path.join(movdir, ARN, extension ),
os.path.join(movdir, newname, extension))
oldpath = os.path.join(root, newname)
print ARN, "to", newname
newpath = basedir + "\\" + newname + subfolder
shutil.copy(oldpath, newpath)
print "Copied"
except:
print ("Error occurred")
Thanks to the answers below here is my final code:
import arcpy
import os
import re
import sys
import traceback
import collections
import shutil
movdir = r"C:\Scans"
basedir = r"C:\Links"
subfolder = "\Private Drain Connections"
import string
l = ['372911000002001',
'372809000001400',
'372809000001500',
'372809000001700',
'37292200000800'
]
l2 = ['372911000003100',
'372909000001400',
'372909000001500',
'372909000001700',
'372922000000800'
]
try:
#Walk through all files in the directory that contains the files to copy
for root, dirs, files in os.walk(movdir):
for filename in files:
#find the name location and name of files
path = os.path.join(root, filename)
#file name and extension
ARN, extension = os.path.splitext(filename)
oldname = str(ARN)
#Location of the corresponding folder in the new directory
link = os.path.join(basedir, ARN)
if not os.path.exists(link):
for ii, jj in zip(l, l2):
newname = re.sub(ii,jj, ARN)
newname = str(newname)
print path
newpath = os.path.join(root, oldname) + extension
print "new name", newpath
os.rename(path, newpath)
print "Renaming"
newpath2 = basedir + "\\" + newname + subfolder
shutil.copy(newpath, newpath2)
print "Copied"
if newname != ARN:
break
else:
continue
except:
print ("Error occurred")
tb = sys.exc_info()[2]
tbinfo = traceback.format_tb(tb)[0]
pymsg = "PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n " + \
str(sys.exc_type)+ ": " + str(sys.exc_value) + "\n"
msgs = "GP ERRORS:\n" + arcpy.GetMessages(2 )+ "\n"
print (pymsg)
print (msgs)
For me the way to go is to read both lists into list objects:
list1 = ["372911000002001", "372809000001400", "372809000001500"]
list2 = ["372911000003100", "372909000001400", "372909000001500"]
for ii, jj in zip(list1, list2):
newname = re.sub(ii,jj,ARN) #re.sub returns ARN if no substitution done
if newname != ARN:
break
An idea: try to convert the id's to strings. I mean:
newname = re.sub('372911000002001','372911000003100',ARN)
Hope it helps!

Categories