0

Relatively new to Python -- I have several csv files at dir2. I only want csv files starting with myfile_xxx.csv . I want to use glob.glob and print names of all csv files available starting with myfile. Here is the code I used

   import glob
   path = "C:/Users/dir1/dir2"
   os.chdir(path)
   print(os.getcwd())
   for filenames in sorted(glob.glob('path/myfile?.csv')):
       print(filenames)

Output doesn't print filenames. What is wrong here ...?

1 Answer 1

1

The ? syntax matches only a single character, so you can instead a string like: glob.glob('path/myfile_???.csv'), assuming the suffix after the underscore is always a three-digit number.

Sign up to request clarification or add additional context in comments.

2 Comments

What should I use instead of ? if I have more than three digit number after underscore ?
@VPER A single asterisk (*) matches any number of characters. You can also create ranges, for instance like so: [0-9]. See: en.wikipedia.org/wiki/Glob_%28programming%29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.