Python - Delete all files EXCEPT for - python

I have a Python script and I'm trying to delete all files in this directory EXCEPT for the .csv file. Getting syntax error on the "not" in this line:
for CleanUp not in glob.glob("c:\python\AIO*.*"):
If I remove the "not", it will delete the AIO.csv file, but I need to preserve that file and ONLY that file. Not clear why it's not working.
import os
import glob
import time
file_path = "c:\python\AIO.csv"
while not os.path.exists(file_path):
time.sleep(10)
if os.path.isfile(file_path):
#Verifies CSV file was created, then deletes unneeded files.
for CleanUp not in glob.glob("c:\python\AIO*.*"):
os.remove(CleanUp)

Try this instead
import os
import glob
import time
file_path = "c:\python\AIO.csv"
while not os.path.exists(file_path):
time.sleep(10)
if os.path.isfile(file_path):
# Verifies CSV file was created, then deletes unneeded files.
for clean_up in glob.glob('C:/python/*.*'):
print(clean_up)
if not clean_up.endswith('AIO.csv'):
os.remove(clean_up)
glob doesn't print any directories, only files, and it also gets the entire path so you can just call os.remove(clean_up). This should work. It works on my machine which is also Windows 7 x64.
I think your problem was that you were looping over the path c:\python\AIO*.* which is a file so it only does one loop and terminates which skips all other files in the directory

An alternate way would be get the list of files (glob.glob returns a list) and then remove the one item you want to preserve.
import os
import glob
import time
file_path = "c:\python\AIO.csv"
while not os.path.exists(file_path):
time.sleep(10)
if os.path.isfile(file_path):
# get list of files that match
cleanupFiles = glob.glob("c:\python\AIO*.*")
cleanupFiles.remove(file_path)
for cleanupFile in cleanupFiles:
os.remove(cleanupFile)

Related

Opening a File Inside Nested Folders With Glob In Python

I am trying to open a file inside the two folders
import glob
import os
wPlayer = '1'
playeritems = 'PlayerFiles/PlayerItems'
with glob.glob(os.path.join(playeritems, open('inventory.%s.txt' % wPlayer, 'r'))) as wPs:
#do stuff with wPs
But it is giving me there error
There is no such file or directory: 'inventory.1.txt'
But I know for a fact that there is 'inventory.1.txt' inside PlayerFiles/PlayerItems.
What am I doing wrong? Is it because it is a string?
I used this question to get where I am now.
If you have the path and the filename, as constructed with your join, what is glob doing there? It looks like you're opening a single file.
import os
wPlayer = '1'
playeritems = 'PlayerFiles/PlayerItems'
with open(os.path.join(playeritems,'inventory.%s.txt' % wPlayer), 'r') as wPs:
#do stuff with wPs

gzip multiple files in python

I have to compress a lot of XML files into and split them by the data in the file name, just for clarification's sake, there is a parser which collects information from XML file and then moves it to a backup folder. My code needs to gzip it according to the date in the filename and group those files in a compressed .gz file.
Please find the code bellow:
import os
import re
import gzip
import shutil
import sys
import time
#
timestr = time.strftime("%Y%m%d%H%M")
logfile = 'D:\\Coleta\\log_compactador_xml_tar'+timestr+'.log'
ptm_dir = "D:\\PTM\\monitored_programs\\"
count_files_mdc = 0
count_files_3gpp = 0
count_tar = 0
#
for subdir, dir, files in os.walk(ptm_dir):
for file in files:
path = os.path.join(subdir, file)
try:
backup_files_dir = path.split(sep='\\')[4]
parser_id = path.split(sep='\\')[3]
if re.match('backup_files_*', backup_files_dir):
if file.endswith('xml'):
# print(time.strftime("%Y-%m-%d %H:%M:%S"), path)
data_arq = file[1:14]
if parser_id in ('parser-924'):
gzip_filename_mdc = os.path.join(subdir,'E4G_PM_MDC_IP51_'+timestr+'_'+data_arq)
with open(path, 'r')as f_in, gzip.open(gzip_filename_mdc + ".gz", 'at') as f_out_mdc:
shutil.copyfileobj(f_in, f_out_mdc)
count_files_mdc += 1
f_out_mdc.close()
f_in.close()
print(time.strftime("%Y-%m-%d %H:%M:%S"), "Compressing file MDC: ",path)
os.remove(path)
except PermissionError:
print(time.strftime("%Y-%m-%d %H:%M:%S"), "Permission error on file:", fullpath, file=logfile)
pass
except IndexError:
print(time.strftime("%Y-%m-%d %H:%M:%S"), "IndexError: ", path, file=logfile)
pass
As long as I seem it creates a stream of data, then compress and write it to a new file with the specified filename. However, instead of grouping each XML file independently inside a ".gz" file, it does creates inside the "gzip" file, a big file (big stream of data?) with the same name of the output "gzip" file, but without any extension. After the files are totally compressed, it's not possible to uncompress the big file generated inside the "gzip" output file. Does someone know where is the problem with my code?
PS: I have edited the code for readability purposes.
Not sure whether the solution is still needed, but I will just leave it here for anyone who faces the same issue.
There is a way to create a gzip archive in python using tarfile, the code is quite simple:
with tarfile.open(filename, mode="w:gz") as archive:
archive.add(name=name_of_file_to_add, recursive=True)
in this case name_of_file_to_add can be a directory, in which case tarfile will add it recursively with all its contents. Obviously you will need to import the tarfile module.
If you need to add files without a directory a simple for with calls to add will do (recursive flag is not required in this case).

How to move from one directory to another and delete only '.html' files in python?

I attended an interview and they asked me to write a script to move from one directory to another and delete only the .html files.
Now I tried to do this at first using os.remove() . Following is the code:
def rm_files():
import os
from os import path
folder='J:\\Test\\'
for files in os.listdir(folder):
file_path=path.join(folder,files)
os.remove(file_path)
The problem I am facing here is that I cannot figure out how to delete only .html files in my directory
Then I tried using glob. Following is the code:
def rm_files1():
import os
import glob
files=glob.glob('J:\\Test\\*.html')
for f in files:
os.remove(f)
Using glob I can delete the .html files but still I cannot figure out how to implement the logic of moving from one directory to another.
And along with that can someone please help me figure out how to delete a specific file type using os.remove() ?
Thank you.
Either of these methods should work. For the first way, you could just string.endswith(suffix) like so:
def rm_files():
import os
from os import path
folder='J:\\Test\\'
for files in os.listdir(folder):
file_path=path.join(folder,files)
if file_path.endswith(".html"):
os.remove(file_path)
Or if you prefer glob, moving directories is fairly straightforward: os.chdir(path) like this:
def rm_files1():
import os
os.chdir('J:\\Test')
import glob
files=glob.glob('J:\\Test\\*.html')
for f in files:
os.remove(f)
Though it seems unnecessary since glob is taking an absolute path anyway.
Your problem can be described in the following steps.
move to specific directory. This can be done using os.chdir()
grab list of all *.html files. Use glob.glob('*.html')
remove the files. use os.remove()
Putting it all together:
import os
import glob
import sys
def remove_html_files(path_name):
# move to desired path, if it exists
if os.path.exists(path_name):
os.chdir(path_name)
else:
print('invalid path')
sys.exit(1)
# grab list of all html files in current directory
file_list = glob.glob('*.html')
#delete files
for f in file_list:
os.remove(f)
#output messaage
print('deleted '+ str(len(file_list))+' files in folder' + path_name)
# call the function
remove_html_files(path_name)
To remove all html files in a directory with os.remove() you can do like this using endswith() function
import sys
import os
from os import listdir
directory = "J:\\Test\\"
test = os.listdir( directory )
for item in test:
if item.endswith(".html"):
os.remove( os.path.join( directory, item ) )

Loop over multiple folders from list with glob.glob

How can I loop over a defined list of folders and all of the individual files inside each of those folders?
I'm trying to have it copy all the months in each year folder. But when I run it nothing happens..
import shutil
import glob
P4_destdir = ('Z:/Source P4')
yearlist = ['2014','2015','2016']
for year in yearlist:
for file in glob.glob(r'{0}/*.csv'.format(yearlist)):
print (file)
shutil.copy2(file,P4_destdir)
I think the problem might be that you require a / in you source path:
import shutil
import glob
P4_destdir = ('Z:/Source P4/')
yearlist = ['2014','2015','2016'] # assuming these files are in the same directory as your code.
for year in yearlist:
for file in glob.glob(r'{0}/*.csv'.format(yearlist)):
print (file)
shutil.copy2(file,P4_destdir)
Another thing that might be a problem is if the destination file does not yet exist. You can create it using os.mkdir:
import os
dest = os.path.isdir('Z:/Source P4/') # Tests if file exists
if not dest:
os.mkdir('Z:/Source P4/')

Remove files by using placeholder

I want to remove several files from a directory using python. The shell command would look like
rm *_some.tex
When I use something like this in python, nothing get's deleted:
intermediates = ('*_some.text', '*_other.text')
for intermediate in intermediates:
if os.path.isfile(intermediate):
os.remove(intermediate)
How can I achieve the shell behavior in python?
you'll need to use glob or fnmatch to properly shell expand globs. Plus if os.path.isfile: os.remove leads to some race conditions. This is nicer:
import glob
globtexts = ('*_some.text', '*_other.text')
files = [glob.glob(globtext) for globtext in globtexts]
# try saying that line out loud five times fast....
for file in files:
try:
os.remove(file)
except Exception as e:
print("There was a problem removing {}: {!r}".format(file, e))
Or, right next to glob in the Python documentation is fnmatch
import fnmatch
import os
for file in os.listdir('.'):
if fnmatch.fnmatch(file, '*_some.text') or fnmatch.fnmatch(file, '*_other.text'':
os.remove(file)
To do this recursively from /home, for example, use os.walk
for root, dirs, files in os.walk('/home'):
for file in files:
if fnmatch.fnmatch(file, '*_some.text') or fnmatch.fnmatch(file, '*_other.text'):
os.remove((root+'/'+file))

Categories