首页 > 解决方案 > 搜索目录中是否存在匹配 4 位数字的文件名

问题描述

我正在检查路径中是否存在文件名 - 'Dev Finance GRM CONS ASPAC_Sales_3069_WI2_2020_GrsSalesAdj_Manual Upload Planning_1000records.csv'

我在同一路径中生成多个具有相似名称的文件,但在上述情况下是唯一的引脚('3069')。此“3069”存储在唯一变量“upload_pin”中

upload_pin = 3069 (我想搜索包含此 upload_pin 的 .csv 文件是否存在于我的路径中)

试过这个:

path = r"\\OPCITSNAPADCON2.jnj.com\tm1\cons\grm_aspac\user_in\Manual Upload Current Plan\Archive"
csv_files = glob.glob(os.path(path + '/*.csv'))
for filename in csv_files:
    if upload_pin in filename:
               print(filename)

标签: pythonpandasglob

解决方案


尝试

# get file names of all csv files in the directory
os.chdir('...')
all_csv_files = glob.glob("*.csv")

# provide your pin
upload_pin = 3069

# for each one of your files
# if the file name contains the target pin, print the file name
for file in all_csv_files:
    if (str(upload_pin) in file):
        print(file)

推荐阅读