Error on deleting file in a python code - python

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!

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)

Python - Iterating over all text files recursively

I am creating a text parser with python 3.6. I have a file layout like below:
(The real file structure I will be using is much more extensive than this.)
-Directory(main folder)
-amerigroup.txt
-bcbs.txt
childfolder
-medicare.txt
I need to extract text into 2 different lists (going through and appending to my ever-growing lists). Whenever I run my current code, I can't seem to get my program to open up my medicare.txt file to read and extract the information. I get an error stating that there is no such file or directory: 'medicare.txt'.
My goal is to get the data from the 3 files and extract it in one go. How do I get the amerigroup and bcbs data then go into the childfolder and get medicare.txt, then repeat that for all branches of my file path?
I am simply trying to open and close my text files in this code snippet. Here's what I have so far:
import re
import os
import pandas as pd
#change active directory
os.chdir(r'\\company\Files\HomeDrive\user\My Documents\claimstest')
#rootdir = r'\\company\Files\HomeDrive\user\My Documents\claimstest'
#set up Regular Expression objects to parse X12
claimidRegex = re.compile(r'(CLM\*)(\d+)')
dxRegex = re.compile(r'(ABK:)(\w\d+)(\*|~)(ABF:)?(\w\d+)?(\*|~)?(ABF:)?(\w\d+)?(\*|~)?(ABF:)?(\w\d+)?(\*|~)?(ABF:)?(\w\d+)?(\*|~)?(ABF:)?(\w\d+)?(\*|~)?(ABF:)?(\w\d+)?(\*|~)?(ABF:)?(\w\d+)?(\*|~)?')
claimids = []
dxinfo = []
for dirpath, dirnames, files in os.walk(topdir):
for name in files:
cid = []
dx = []
if name.lower().endswith(exten):
data = open(name, 'r')
data.close()
Thank you so much for taking your time to assist me on this!
edit: I have tried using walk to no avail so far. My most recent attempt (I tried using txtfile_full_path as well--did not work):
for dirpath, dirnames, filename in os.walk(base_dir):
for filename in filename:
#defining file type
txtfile=open(filename,"r")
txtfile_full_path = os.path.join(dirpath, filename)
print(filename)
edit2 for anyone interested. This was my final solution to the problem:
import re
import os
import pandas as pd
#change active directory
os.chdir(r'\\company\Files\HomeDrive\user\My Documents\claimstest')
base_dir = (r'\\company\Files\HomeDrive\user\My Documents\claimstest')
#set up Regular Expression objects to parse X12
claimidRegex = re.compile(r'(CLM\*)(\d+)')
dxRegex = re.compile(r'(ABK:)(\w\d+)(\*|~)(ABF:)?(\w\d+)?(\*|~)?(ABF:)?(\w\d+)?(\*|~)?(ABF:)?(\w\d+)?(\*|~)?(ABF:)?(\w\d+)?(\*|~)?(ABF:)?(\w\d+)?(\*|~)?(ABF:)?(\w\d+)?(\*|~)?(ABF:)?(\w\d+)?(\*|~)?')
claimids = []
dxinfo = []
for dirpath, dirnames, filename in os.walk(base_dir):
for filename in filename:
txtfile_full_path = os.path.join(dirpath, filename)
x12 = open(txtfile_full_path, 'r')
for i in x12:
match = claimidRegex.findall(i)
for word in match:
claimids.append(word[1])
x12.seek(0)
for i in x12:
match = dxRegex.findall(i)
for word in match:
dxinfo.append(word)
x12.close()
datadic = dict(zip(claimids, dxinfo))
You need to pass the full path to open. Just creating a string variable somewhere won't do anything for you! So the following should avoid your error:
txt_list = []
for dirpath, dirnames, filename in os.walk(base_dir):
for filename in filename:
# create full path
txtfile_full_path = os.path.join(dirpath, filename)
with open(txtfile_full_path) as f:
txt_list.append(f.read())
It should be easy enough to integrate the segregation based on your regexes now...

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()

not listing entire directory

Im new on Python, Im actually on a short course this week, but I have a very specific request and I dont know how to deal with it right now: I have many different txt files in a folder, when I use the following code I receive only the filename of two of the many files, why is this?
regards!
import dircache
lista = dircache.listdir('C:\FDF')
i = 0
check = len(lista[0])
temp = []
count = len(lista)
while count != 0:
if len(lista[i]) != check:
temp.append(lista[i- 1])
check = len(lista[i])
else:
i = i + 1
count = count - 1
print (temp)
Maybe you can use the glob library: http://docs.python.org/2/library/glob.html
It seems that it works UNIX-like for listing files so maybe it can work with this?
import glob
directory = 'yourdirectory/'
filelist = glob.glob(directory+'*.txt')
If I've understood you correct, you would like to get all files?
Try it in this case:
import os
filesList = None
dir = 'C:\FDF'
for root, dirs, files in os.walk(dir):
filesList = files
break
print(filesList)
If need full path use:
import os.path
filesList = None
dir = 'C:\FDF'
for root, dirs, files in os.walk(dir):
for file in files:
filesList.append(os.path.join(root, file))
print(filesList)

Copy Files From List 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)

Categories