How to remove numbers from strings in a list using one single for loop in python3?

Alex Stelmakh

Have this code where I'm trying to remove all the numerical characters from strings in file names. It seems everything going all right but file names don't change.

import os

files_list = os.listdir('/mnt/c/Users/maverick/Desktop/prank/prank')
for file_name in files_list:
    count = 0
    l = []
    for char in file_name:
        if not char.isdigit():
            l.append(char)
    new_string = ''.join(l)
    print('New string: ' + new_string)
    print('Old file name: ' + file_name)
    file_name = new_string
    print('New file name: ' + file_name)
    count = count + 1

print(files_list)
Sreeram TP

If you want to rename you can use os.rename(). Also, to remove all numbers from the file names you can use maketrans on digits from string like this.

import os
from string import digits

path = '/mnt/c/Users/maverick/Desktop/prank/prank'

files_list = os.listdir(path)
new_files_list = []

remove_digits = str.maketrans('', '', digits)

for file_name in files_list:
    count = 0
    new_string = file_name.translate(remove_digits)
    new_files_list.append(new_string)
    print('New string: ' + new_string)
    os.rename(os.path.join(path, file_name), os.path.join(path, new_string))
    print('Old file name: ' + file_name)
    file_name = new_string
    print('New file name: ' + file_name)
    count = count + 1

print('Old Filenames :', files_list)
print('New Filenames :', new_files_list)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to use filter() to remove all strings from a list with strings and numbers?

Remove numbers from a list of strings

Python remove one worded strings from list

How to print list of list into one single list in python without using any for or while loop?

How to remove matching elements from one list using another? Python

Is it possible to remove all single quotes from a list of strings in python?

How to remove single quote from a list in python

How to remove punctuation marks from a list of strings without using libraries in Python 3.x?

Creating a string with separators from a list of numbers using Python3

How to cast the output of a for loop into a single list of strings in Python?

Deleting a string from a one list with strings/numbers

how to remove composite numbers from a list in python 3?

list with numbers and strings to single string python

Python Remove List of Strings from List of Strings

Python3 filter/remove strings of a list

remove specific strings from a string using a loop

Remove numbers from result; Python3

How to pass a list of strings one by one to query() using a function in python?

remove the last two characters from strings in list using python

How to remove list of words from a list of strings?

How to remove strings in list from another list?

How to remove a list of substrings from a list of strings

How to print all strings from the list specified number of times using nested loop in python

How to append multiple strings from a single input into a list in Python

How to remove elements from list in array without using for-loop

How to remove element from list without using for loop?

Remove items from one list if they contain strings from another list

Remove characters from list of strings using comprehension

How to remove a list from one number to another using jquery?