我正在使用glob檢索檔案夾(“資料”)中所有檔案的路徑:
import glob
import geopandas
for file in glob.glob(r"/home/data/*.txt"):
sf = geopandas.read_file(file)
但是,在這里,我想知道如何僅檢索名稱在變數中作為串列列出的選定檔案。例如,我只想要以下檔案的路徑:aaa.txt、bdf.txt、hgr.txt,在“data”檔案夾中,這些檔案在變數“imp”中列出。
imp = ['aaa.txt', 'bdf.txt', 'hgr.txt']
uj5u.com熱心網友回復:
像這樣的東西可以做到。只需回圈瀏覽您需要的檔案。
import geopandas
imp = ['aaa.txt', 'bdf.txt', 'hgr.txt']
for file in imp:
sf = geopandas.read_file(f"/home/data/{file}")
uj5u.com熱心網友回復:
像這樣?
import geopandas
path = "/home/data/"
imp = [ 'aaa.txt', 'bdf.txt', 'hgr.txt']
for file in imp:
if os.path.exists(path file):
sf = geopandas.read_file(path file)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/349377.html
