user3932077
user3932077

Reputation: 77

Python 3.5:Not able to remove non alpha -numeric characters from file_name

i have written a python script to rename all the files present in a folder by removing all the numbers from the file name but this doesn't work . Note :Same code works fine for python2.7

import os

def rename_files():
    #(1) get file names from a folder
    file_list = os.listdir(r"D:\prank")
    print(file_list)
    saved_path = os.getcwd()
    print("Current working Directory is " + saved_path)
    os.chdir(r"D:\prank")
    #(2) for each file ,rename filename
    for file_name in file_list:
        os.rename(file_name, file_name.translate(None,"0123456789"))
rename_files()

Can anyone tell me how to make it work.Is the translate function which is not working properly

Upvotes: 0

Views: 223

Answers (3)

user94559
user94559

Reputation: 60143

Others have pointed out other issues with your code, but as to your use of translate, in Python 3.x, you need to pass a dictionary mapping ordinals to new values (or None). This code would work:

import string
...
file_name.translate(dict(ord(c), None for c in string.digits))

but this seems easier to understand:

import re
...
re.sub(r'\d', '', file_name)

Upvotes: 0

N.P
N.P

Reputation: 64

The problem is with os.rename() portion of your code.

os.rename() requires you to give it a full path to the file/folder you want to change it to, while you only gave it the file_name and not the full path.

You have to add the full path to the folders/files directory. so it should look like this:

def rename_files():

# add the folder path

    folder_path = "D:\prank\\"
    file_list = os.listdir(r"D:\prank")
    print(file_list)
    saved_path = os.getcwd()
    print("Current working Directory is " + saved_path)
    os.chdir(r"D:\prank")

    # Concat the folder_path with file_name to create the full path.

    for file_name in file_list:
        full_path = folder_path + file_name
        print (full_path) # See the full path here.
        os.rename(full_path, full_path.translate(None, "0123456789"))

Upvotes: 1

Cid-El
Cid-El

Reputation: 510

look up the documentation for os, heres what ive found on rename:

os.rename(src, dst, *, src_dir_fd=None, dst_dir_fd=None)

Rename the file or directory src to dst. If dst is a directory, OSError will be raised. On Unix, if dst exists and is a file, it will be replaced silently if the user has permission. The operation may fail on some Unix flavors if src and dst are on different filesystems. If successful, the renaming will be an atomic operation (this is a POSIX requirement). On Windows, if dst already exists, OSError will be raised even if it is a file.

This function can support specifying src_dir_fd and/or dst_dir_fd to supply paths relative to directory descriptors.

If you want cross-platform overwriting of the destination, use replace().

New in version 3.3: The src_dir_fd and dst_dir_fd arguments.

heres a link to the documentation, hope this helps, thanks

https://docs.python.org/3/library/os.html

Upvotes: 0

Related Questions