python - 搜索目录中是否存在匹配 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)
解决方案
尝试
# 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)
推荐阅读
- json - Firefox 书签.json 文件规范
- javascript - 如何在 Javascript 中使用 ComputedStyle 获取边框宽度?
- python - 如何通过使用条件查找和替换数据框中的值来更新?
- javascript - 当我回到它时,照片又被喜欢了
- docker - 使用 Go 部署 Docker 的问题
- r - 如何使用 Windows 10 和 R 3.4.4 安装和调用包“multidplyr”
- php - PHP连接到数据库失败且没有错误
- excel - 循环使用白色填充的所有单元格并将它们更改为无填充
- html - 如何在 Dreamweaver 中的 HTML 电子邮件模板中的背景图像上方的图像上去掉 X?
- python - Tkinter:Win check in 可扩展井字游戏不起作用