Python Forum
Convert each element of a list to a string for processing
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert each element of a list to a string for processing
#1
Greetings!
I'm trying to remove a bunch of test files using 'glob'.
I thought I could do it like this:
prod_tod_to_Rem = glob.glob("C:/0001/**/*.txt", recursive=True)   
dev_tod_to_Rem =glob.glob("C:/04/**/*.txt",recursive=True)        
rm_lst = [prod_tod_to_Rem,dev_tod_to_Rem]

tor = [str(i) for i in rm_lst]
print(tor)
try:
    os.remove(tor)
    print (f" Removing TXT Flies --> {tor}")
except OSError as e:
    print("Error: CANNOT Remove TXT file -->>", e )
for some reason this part of the snippet not converting the list element to a string.
tor = [str(i) for i in rm_lst]
Thank you.
Reply
#2
glob.glob returns list of strings, so converting to string once more is redundant.

In your code you converting list to string, not list elements. You need to unpack first if you want to convert items in list:

>>> spam = ['bacon', 'eggs']
>>> ham = [42, 2021]
>>> [str(i) for i in [spam, ham]]
["['bacon', 'eggs']", '[42, 2021]']
>>> [str(i) for i in [*spam, *ham]]
['bacon', 'eggs', '42', '2021']
However, in your case and if I understood your objective correctly, you should just unpack:

rm_lst = [*prod_tod_to_Rem, *dev_tod_to_Rem]  
tester_V likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
I'm trying to remove/clean a bunch of directories using 'glob.glob'
I do not want to do 10 'try/except' for each directory.
I thought I can make a list of directories
prod_tod_to_Rem = glob.glob("C:/0001/**/*.txt", recursive=True)   
dev_tod_to_Rem =glob.glob("C:/04/**/*.txt",recursive=True)        
rm_lst = [prod_tod_to_Rem,dev_tod_to_Rem]
and will pass them to the try/except one by one.

I tried
rm_lst = [*prod_tod_to_Rem, *dev_tod_to_Rem]  
it does the same thing like the
rm_lst = [prod_tod_to_Rem,dev_tod_to_Rem]
 
tor = [str(i) for i in rm_lst]
print(tor)
I might be confused and do not understand your suggestion...
thank you.
Reply
#4
If I wanted to remove all the .odt files in a directory and or subdirectories:

import glob, os

path = '/home/pedro/summer2021/**/'

all_files = glob.glob(path + '*.odt')

# careful with this, there is no going back
for a in all_files:
    os.remove(a)
Reply
#5
(Jun-14-2021, 08:45 PM)tester_V Wrote: I thought I can make a list of directories
Yes you can here a example where make a list then iterate over with iglob
from glob import iglob
import os

folders = [r'G:\div_code\1', r'G:\div_code\2']
for folder in folders:
    for f_name in iglob(f"{folder}/**/*.txt", recursive=True):
        os.remove(f_name) 
tester_V likes this post
Reply
#6
You can also write a glob function that takes several patterns
from glob import iglob
from itertools import chain

def multiglob(ipattern, recursive=False):
    return chain.from_iterable(iglob(p, recursive=recursive) for p in ipattern)

for tor in multiglob(("C:/0001/**/*.txt", "C:/04/**/*.txt"), recursive=True):
    try:
        ...
tester_V likes this post
Reply
#7
iglob
I did not know about it!
Thank you guys!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert string to path using Python 2.7 tester_V 10 597 Nov-20-2021, 02:20 PM
Last Post: snippsat
  sorting a list of lists by an element leapcfm 3 390 Sep-10-2021, 03:33 PM
Last Post: leapcfm
  convert numbers into list lokesh 1 827 Jun-03-2021, 06:37 AM
Last Post: menator01
Question convert unlabeled list of tuples to json (string) masterAndreas 4 2,573 Apr-27-2021, 10:35 AM
Last Post: masterAndreas
  Convert String of an int array to a Numpy array of ints mdsousa 5 1,865 Apr-08-2021, 08:00 PM
Last Post: mdsousa
Star Convert Bytearray into List using list() Shlok 2 875 Feb-18-2021, 10:44 AM
Last Post: deanhystad
  convert List with dictionaries to a single dictionary iamaghost 3 1,108 Jan-22-2021, 03:56 PM
Last Post: iamaghost
  Convert string to JSON using a for loop PG_Breizh 3 1,077 Jan-08-2021, 06:10 PM
Last Post: PG_Breizh
Sad Convert python list to dictionary akanowhere 6 1,369 Dec-27-2020, 09:26 PM
Last Post: Pedroski55
  Convert hex value into string Sancho_Pansa 8 1,564 Dec-23-2020, 08:54 AM
Last Post: Sancho_Pansa

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020