Home > OS >  Finding files in a directory that have an underscore and no extension and adding an extension to it
Finding files in a directory that have an underscore and no extension and adding an extension to it

Time:01-14

I have a directory with some files with an underscore and no extension that I would like to add an extension to it and get rid of the underscore.

Example: list of files in directory

filename1_.jpg
file_name_2_
file_name3

The renamed files should be changed to look like whats below:

file_name_2.jpg
file_name3.jpg

I was going to start with just looking for files with no extension but that just list all the files.

Code I tried below:

# Finding files with extension using for loop
#import os module
import os
# Specifies the path in path variable
path = "/tmp/0/jpg/"
for i in os.listdir(path):
    # List files with extension
    if i.endswith(""):
        print("Files with extension no extension:",i)

PS: I'm trying to do this all in python since it's going to be used in another piece of python code.

CodePudding user response:

The issue with if i.endswith("") is that it lists every single file. What you want to do is check for every file that ends in an underscore, and then double check it has no extension. This is my method:

import os
path = r"your path"
for i in os.listdir(path):
    if i.endswith("_"):
        if os.path.splitext(i)[1] == "":
            print("Files with extension no extension:",i)
            os.rename(str(i), f"{str(i)[:-1]}.jpg")

The beginning is what you did. However, I then check to see if it ends with an _. After that, the line os.path.splitext(i)[1] basically recieves the extension of the file, and we check to make sure their is no extension (ie extension = ""). Finally, we use the os.rename() function to rename the file.

str(i)[:-1] basically removes the last character of the file, which in this case is the underscore. We then add a .jpg extension to it, and now all the files have been renamed.

CodePudding user response:

I've reused your snippet with the specified usecase:

# Finding files with extension using for loop
#import os module
import os
# Specifies the path in path variable
path = "/tmp/0/jpg/"
names = []
for i in os.listdir(path):
    # List files with extension
    # this will handle the files with _ at the end and does not have an extension
    if i.endswith("_") or not i.endswith('.jpg'): 
        names.append(f'{i.replace("_","")}.jpg')
    # this will handle the files that contains _ and ends with an extension
    elif '_' in i and i.endswith('.jpg'):
        names.append(f'{i.replace("_", "")}')

 print(names)

Output:

['filename2.jpg', 'filename3.jpg', 'filename1.jpg']

CodePudding user response:

You can use the regex library re:

import os
import re

path = '<Your\\Path\\>'
for file in os.listdir(path):
    if not(re.search(r'\..*', file)) and os.path.isfile(path   file):
        new_name = re.sub('_$', '', file)
        new_name  = '.jpg'
        os.rename(file, new_name)

Explanation:

  1. We escape the dot symbol \.
  2. We match anything (every character) afterwards
  3. The os.path.isfile makes sure you won't rename folders, because they are being listed as well in the os.listdir() method
  4. Using re.sub in order to remove the _ in file names that end with this character (preserving it if it shows anywhere else in the name)

If we find matches, it means the file is in the format random.something and it If it finds something, we can skip it as the file's name has an extension.


If you only meant to change the name of files that don't end with .jpg you can change the regex pattern in my code to \.jpeg

  • Related