-1

I have this function here that renames (if necessary) all files in a given folder and returns all filenames as absolute paths:

I forgot to mention: filepath is always an absolute path.

import re
import os

# This function renames all files in a given
# folder and return them as absolute paths
def rename_get_Files(filepath):
    files = os.listdir(filepath)
    files_list = []
    counter = 1
    files_renamed = 0
    for filename in files:
        # If the file does not start with 'Offer_Pic_' > rename it and add to the files_list
        if not re.search('Offer_Pic_.+', filename):
            file_name, file_extension = os.path.splitext(filepath+filename)
            print(file_extension)

            new_filename = "Offer_Pic_" + str(counter) + file_extension
            old_filename = filepath + filename
            new_filename = filepath + new_filename
            # rename() function will
            # rename all the files
            os.rename(old_filename, new_filename)
            counter += 1
            print(f'This is the new filename: ' + new_filename)
            files_list.append(new_filename)
            files_renamed += 1
        else:
            # Append the absolute paths of all already correctly named files to the files_list
            files_list.append(os.path.abspath(filename))
    print(files_list)
    print(f'We have renamed ' + str(files_renamed) + ' files.')
    return files_list

However, when I call the function from another one to use these absolute paths:

pictures = rename_get_Files(filepath)
print(pictures)

... it returns the paths being inside the script's working directory.

Because of that, the next function of my script crashes because it assumes that the files are in the working directory of the script - which they are not as they were not moved from their initial location (filepath).

Please help me to keep the absolute file paths.

I tried to regain the absolute path of the pictures, but the wrong one (inside script dir keeps being returned)

for pic in pictures:
    abs_path_pic = os.path.abspath(pic)
    print(pic)
    print(abs_path_pic)
    pictureBox.send_keys(abs_path_pic)
  • The first path of the `if` `else` does not seem to ensure paths are absolute. Why do you assume otherwise? Can you please [edit] the question to provide a [mre], i.e. some sample input/output showing the effect? It also seems the code could be cut down to focus on the core issue, making it more useful for others. – MisterMiyagi Dec 31 '22 at 11:38
  • Sorry for that, I should have mentioned in the first place that filepath is only give absolute. That of course you cannot know. I will edit my post accordingly. – Jakob Czapski Dec 31 '22 at 14:01

1 Answers1

0

I found the error: initially no absolute path was returned by os.

I added to the end of the function:

path = os.path.abspath(filepath)
files_list = [entry.path for entry in os.scandir(path) if entry.is_file()]

The whole function now looks like this:

    # This function renames all files in a given folder and return them as absolute paths
def rename_get_files(filepath):
    print(f'this is the given filepath: {filepath}')

    files = os.listdir(filepath)

    files_list = []
    print(f'these are our initial files: {files}')
    counter = 1
    files_renamed = 0
    for filename in files:
        # If the file does not start with 'Offer_Pic_' > rename it and add to the files_list
        if not re.search('Offer_Pic_.+', filename):
            file_name, file_extension = os.path.splitext(filepath+filename)
            print(file_extension)

            new_filename = "Offer_Pic_" + str(counter) + file_extension
            old_filename = filepath + "/" + filename
            print(old_filename)
            new_filename = filepath + "/" + new_filename
            print(new_filename)
            # rename() function will
            # rename all the files
            os.rename(old_filename, new_filename)
            counter += 1
            print(f'This is the new filename: ' + new_filename)
            files_list.append(os.path.abspath(new_filename))
            files_renamed += 1
        else:
            # Append the absolute paths of all already correctly named files to the files_list
            files_list.append(os.path.abspath(filename))
    print(f'We have renamed ' + str(files_renamed) + ' files.')

    path = os.path.abspath(filepath)
    files_list = [entry.path for entry in os.scandir(path) if entry.is_file()]
    print(f'this is our files_list, no files were renamed: {files_list}')
    return files_list

Now it works perfectly! Thank you all for your help!