简体   繁体   中英

How to list all user specified files using glob in python

This is what I am trying to accomplish: In my dir, there are many files but they all follow a format

aaa001 aaa002 aaa003 bbb001 bbb002 bbb003 ccc001 ccc002 ccc003 etc.

What I'm trying to do is get the "family of files" that a user specifies, meaning, I will read input from the user as to which file family they want (eg if the user enters aaa, I want to list out aaa001, aaa002, aaa003)

The command I'm trying to use/run is

files = glob.glob("%userInput.*\.csv")

userInput is the variable I'm using to store the user's selection.

However, it doesn't seem to be getting anything, which means I'm not using the command syntax correctly. Can someone shed some light on what I'm doing wrong? Thanks.

There's an older (2.x) string formatting trick that goes like this:

files = glob.glob("%s*.csv" % userInput)

In newer versions of python (especially 3.x) you want something like:

files = glob.glob("{}*.csv".format(userInput))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM