2

What is required?

Test if any of *.csv file is generated in the current directory. Note that the csv file is named after date/time so its not possible to get the file name in this case.

Problem

Tried the os.path.isfile([exact_path_to_file]) and it works. However what we need to find is if any one of the .csv file is generated if so, assertTrue else assertFalse. In case of assertTrue, will delete the file. Is this possible with python?

Reference

The closest of this is using a regular expression like this post however for this simple check is it really required to go for a regular expression?

Triguna
  • 504
  • 6
  • 13
  • Possible duplicate https://stackoverflow.com/questions/2299454/how-do-quickly-search-through-a-csv-file-in-python – RonicK May 16 '18 at 09:51
  • @RonicK I do not see it as a duplicate. The intention of that post is to import multiple csv to database... Here I just want to check if we can find out in the local file system if there exists any file with extension .csv. – Triguna May 16 '18 at 11:05

2 Answers2

5

Use the glob module to list files in a directory matching a pattern:

import glob
import os.path

csv_files = glob.glob(os.path.join(directory_name, '*.csv'))

If csv_files is a non-empty list, there are matching files.

Under the hood, the glob module transforms the glob pattern to a regular expression for you (via fnmatch.translate(), runs os.listdir() on the given directory, and returns only those names that match the pattern, as full paths:

>>> import os.path, glob, tempfile
>>> with tempfile.TemporaryDirectory() as directory_name:
...     pattern = os.path.join(directory_name, '*.csv')
...     # nothing in the directory, empty glob
...     print('CSV file count:', len(glob.glob(pattern)))
...     # create some files
...     for n in ('foo.csv', 'bar.txt', 'ham.csv', 'spam.png'):
...         __ = open(os.path.join(directory_name, n), 'w')  # touches file, creating it
...     csv_files = glob.glob(pattern)
...     print('CSV file count after creation:', len(csv_files))
...     for filename in csv_files:
...         print(filename)
...
CSV file count: 0
CSV file count after creation: 2
/var/folders/vh/80414gbd6p1cs28cfjtql3l80000gn/T/tmp2vttt0qf/foo.csv
/var/folders/vh/80414gbd6p1cs28cfjtql3l80000gn/T/tmp2vttt0qf/ham.csv
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thanks. This works really well. Good explanation as well. However I would have to choose the other answer for this post which does not include to import any more modules except the system imports. – Triguna May 16 '18 at 11:11
  • @Triguna: `glob` is in the *standard library*, just like `os` and `os.path` are. But sure, under the hood, the implementation is not much different from what the other answer proposes, but that version only covers the 'matching extension' case, while mine covers the general pattern matching case. For example, the moment you want to extend your pattern to include a prefix such as `foobar*.csv`, matching the various parts manually becomes more cumbersome without `glob.glob()`. – Martijn Pieters May 16 '18 at 11:22
  • Thank you. Yes that is a good point. Your answer will stay as highly voted as your answer is what most people look for however for the question I have asked the above answer is sufficient. – Triguna May 16 '18 at 11:30
  • @Triguna: just to be clear: you are entirely right to pick the answer that helped *you* most to award the mark to. :-) – Martijn Pieters May 16 '18 at 12:10
1

You can use os.listdir to get all file names and os.path.splitext to get file extension

any(os.path.splitext(f)[1] == '.csv' for f in os.listdir(path))

For current path, path=os.getcwd(), path='.' will both do (or even leave out the parameter). To delete all *.csv files, just go through a loop

for f in os.listdir('.'):
    if os.path.splitext(f)[1] == '.csv':
        os.remove(f)
jf328
  • 6,841
  • 10
  • 58
  • 82
  • Thanks this as well works and does not need to import any third party modules. – Triguna May 16 '18 at 11:11
  • I would also change the path to os.getcwd() since it is mentioned in the question as search in current directory. Can you improve the answer? – Triguna May 16 '18 at 11:14