在以下目录中有100个zip文件。如何从目录中搜索指定的zip文件。搜索条件可以是zip的'date'
和'salary'
。例如文件abc_19980821_salary.zip
,该文件属于日期21-08-1998。
请建议如何搜索这样的文件夹:
C:Userscountry
abc_19980821_salary.zip
xyz_20200829_salary.zip
pqr_20050620_salary.zip
stu_19990815_salary.zip
klm_20040722_salary.zip
我想搜索pqr_20050620_salary.zip
:
with zp.ZipFile('C:/Users/country/pqr_20050620_salary.zip') as z:
with z.open("file.TXT") as f:
# read the dataset
df = pd.read_csv(f, delimiter = "t")
print(df.head())
您可以使用glob来使用通配符搜索文件。
import os
import glob
import datetime
top = r"C:UsersjezequielDesktop"
d = datetime.date(1998, 8, 21)
ds = d.strftime('%Y%m%d')
pattern = f'*_{ds}_salary.zip' # use wildcards (*)
# make sure we have a match
with open(f'foobar_{ds}_salary.zip', 'wb') as f:
pass
print(glob.glob(os.path.join(top, pattern)))
查找带有某些名称的fname
import os
from datetime import datetime
salary = 9000
date = datetime(2021, 3, 9)
data_fnames = os.listdir('.')
for fname in data_fname:
_, date_raw, sal = fname.split('_')
dt = datetime.strptime(date_raw, "%Y%m%d")
if dt == date and sal == salary:
with zp.ZipFile('C:/Users/country/pqr_20050620_salary.zip') as z:
with z.open("file.TXT") as f:
df = pd.read_csv(f, delimiter = "t")
break
也可以使用正则表达式