Search code examples
pythonpython-3.xos.path

Using a wildcard in the path with os.path.getsize() returns error: OSError


import os

csv = "./CSV/*.csv"

os.path.getsize(csv)

I have 1 file under the directory /CSV that is a .csv file. I want to get the size of that file. I don't want to use the name of the file in the code because the .csv file will change regularly. Currently returns the error:

OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: './CSV/*.csv'

I've tried a few different methods, including using glob, most of the time I only manage to return the name of the file instead of the actual file itself.

Any help will be really appreciated. Thanks.


Solution

  • Use the glob library

    import os
    import glob
    
    csv_files = glob.glob('./CSV/*.csv') #THIS RETURNS A LIST EVEN IF THERE IS A SINGLE MATCH
    for csv_file in csv_files:
        print(os.path.getsize(csv_file))