如何从目录中搜索特定的 zip 文件?
How to search for specific zip file from the directory?
我在以下目录中有 100 个 zip 文件。如何从目录中搜索特定的 zip 文件。搜索条件可以是 zip 的 'date'
和 'salary'
。例如文件abc_19980821_salary.zip
,这个文件属于日期21-08-1998.
请建议如何搜索这样的文件夹:
C:\Users\country
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())
用某个名字搜索 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
或者,您可以使用正则表达式。
您可以使用 glob 使用通配符搜索文件。
import os
import glob
import datetime
top = r"C:\Users\jezequiel\Desktop"
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)))
我在以下目录中有 100 个 zip 文件。如何从目录中搜索特定的 zip 文件。搜索条件可以是 zip 的 'date'
和 'salary'
。例如文件abc_19980821_salary.zip
,这个文件属于日期21-08-1998.
请建议如何搜索这样的文件夹:
C:\Users\country
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())
用某个名字搜索 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
或者,您可以使用正则表达式。
您可以使用 glob 使用通配符搜索文件。
import os
import glob
import datetime
top = r"C:\Users\jezequiel\Desktop"
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)))