2015-05-08 28 views
0

這是我的代碼,它可以在目錄中找到.c文件。我想將它們追加到cfiles列表理解中。所以我可以像cfiles [1],cfiles [2]那樣訪問它們...將文件名添加到列表理解

for files in os.listdir(input_arg): 
    if files.endswith(".c"): 
     cfiles= [((os.path.abspath(input_arg)+"/"+files))] 

這隻會將第一個找到的項目添加到cfiles。在這種情況下應該如何看待for循環?

回答

0

假設input_arg已定義:

cfiles = [os.path.abspath(input_arg)+"/"+files for files in os.listdir(input_arg) if files.endswith(".c")]