1

I'm putting together a Python script that uses pandas to read data from a CSV file, sort and filter that data, then save the file to another location.

This is something I have to run regularly - at least weekly if not daily. The original file is updated every day and is placed in a folder but each day the file name changes and the old file is removed so there is only one file in the directory.

I am able to make all this work by specifying the file location and name in the script, but since the name of the file changes each day, I'd rather not have to edit the script every every time I want to run it.

Is there a way to read that file based solely on the location? As I mentioned, it's the only file in the directory. Or is there a way to use a wildcard in the name? The name of the file is always something like: ABC_DEF_XXX_YYY.csv where XXX and YYY change daily.

I appreciate any help. Thanks!

Mihai Chelaru
  • 7,614
  • 14
  • 45
  • 51
TheAce
  • 23
  • 1
  • 3
  • 1
    [This stack overflow question](https://stackoverflow.com/q/3964681/8206432) answers how to do this with a .txt file. The process should be the same with a .csv file. – Jack Moody Dec 21 '18 at 16:44
  • 1
    Possible duplicate of [Find all files in a directory with extension .txt in Python](https://stackoverflow.com/questions/3964681/find-all-files-in-a-directory-with-extension-txt-in-python) – Wayne Phipps Dec 21 '18 at 18:54

3 Answers3

3
from os import listdir

CSV_Files = [file for file in listdir('<path to folder>') if file.endswith('.csv')

If there is only 1 CSV file in the folder, you can do

CSV_File = CSV_Files[0]

afterwards.

L. B.
  • 430
  • 3
  • 14
  • Thank you all, for the responses! I was able to get this to work now. Really appreciate the help! – TheAce Dec 26 '18 at 15:04
1

Assume that dirName holds the directory holding your file.

A call to os.listdir(dirName) gives you files or child directories in this directory (of course, you must earlier import os).

To limit the list to just files, we must write a little more, e.g.

[f for f in os.listdir(dirName) if os.path.isfile(os.path.join(dirName, f))]

So we have a full list of files. To get the first file, add [0] to the above expression, so

fn = [f for f in os.listdir(dirName) if os.path.isfile(os.path.join(dirName, f))][0]

gives you the name of the first file, but without the directory.

To have the full path, use os.path.join(dirname, fn)

So the whole script, adding check for proper extension, can be:

import os

dirName = r"C:\Users\YourName\whatever_path_you_wish"
fn = [f for f in os.listdir(dirName)\
    if f.endswith('.csv') and os.path.isfile(os.path.join(dirName, f))][0]
path = os.path.join(dirName, fn)

Then you can e.g. open this file or make any use of them, as you need.

Edit

The above program will fail if the directory given does not contain any file with the required extension. To make the program more robust, change it to something like below:

fnList = [f for f in os.listdir(dirName)\
    if f.endswith('.csv') and os.path.isfile(os.path.join(dirName, f))]
if len(fnList) > 0:
    fn = fnList[0]
    path = os.path.join(dirName, fn)
    print(path)
    # Process this file
else:
    print('No such file')
Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41
1

To get the file names solely based on the location:

import os, glob
os.chdir("/ParentDirectory")
for file in glob.glob("*.csv"):
    print(file)
pizza lover
  • 503
  • 4
  • 15