pythonpandasgloblistdir

Is there a way to change a csv filename by its file type in python?


I’ve created a script for a process that I want to automate. I have managed to get the script to download and move the required .csv file into my project file. It’s been moved here in order for the data to be manipulated using pandas.

I now need pandas to access the file but I need to either change the file name according to the file type (.csv) or have pandas access the file type as opposed to the file name.

This is because, every-time this .csv file is downloaded, it has a different name, and in order to use the pandas.read_csv function, the bot would need the file to have the same name every time so it could repeat the process.

I’ve tried to use the glob.glob to access the file according to its file type but can’t seem to change the name of this file. Help me pleaseeeee

Might be also worth mentioning, I’m running this code on a Mac in VSC

#This is the first code I tried to rename the .csv file

import glob
path = (glob.glob("/Users/MANI/Documents/Selenium/*.csv"))
os.rename(‘path’, ‘CC.csv’)

#This is the second code I tried to get pandas to read all .csv files in my chosen directory

import pandas as pd
import glob

path = (‘/Users/MANI/Documents/Selenium/*.csv’) 
all_files = glob.glob(path + "/*.csv")

li = []

for filename in all_files:
    df = pd.read_csv(filename, index_col=None, header=0)
    li.append(df)

frame = pd.concat(li, axis=0, ignore_index=True)

#This is the last code I tried to rename the .csv file. I used part of the code that I used in order to access and move the .csv file from my downloads into my project folder

sourcepath='/Users/MANI/Downloads/'
destinationpath = ‘CC.csv’
sourcefiles = os.listdir(sourcepath)
for file in sourcefiles:
    if file.endswith('.csv'):

os.rename(os.path.join(sourcepath,file), os.path.join(destinationpath,file))

I just need one of these to work but would like to know which one would be the best to go with


Solution

  • This is pretty straight-forward. Here is an example, which I tested and it works:

    import glob
    import os
    filename = glob.glob('*.csv')[0]
    os.rename('./{}'.format(filename), 'correct_name.csv')
    

    Explanation:

    Just keep in mind that the glob returns an array of all the matching filenames. So:

    1. if you know you have more than one .csv, you have to narrow your filter to only match the one that you need
    2. The rename function needs the file path, not just name. So notice that I've prepended the filename with './' to make it into a relative path.