In [1]
#判断输入字符串是否在路径中函数
import os
def findfile2(keyword):
keyword="%.9d" % int(keyword)
judgeExist=False
root=os.path.join('C:\\Users\\96502\\kepler', keyword[0:4])
if os.path.exists(root):
filelist=[]
for root,dirs,files in os.walk(root):
for name in files:
fitfile=filelist.append(os.path.join(root, name))
for i in filelist:
if os.path.isfile(i):
#print(i)
if keyword in os.path.split(i)[1]:
judgeExist=True
#else:
#print('......no keyword!')
return judgeExist
In [2]
#从csv中读取kepid调用findfile2函数
import pandas as pd
import numpy as np
df = pd.DataFrame(pd.read_csv("C:\\Users\\96502\\kepler\\dr24_tce_500.csv"),columns=['kepid'])
print(df)
kep_id=df.to_numpy()
print(kep_id[0])
my_array=np.empty([kep_id.size,1],dtype = str)
filelist=[]
for i in range(kep_id.size):
temp=findfile2(kep_id[i])
my_array[i]=temp
print("处理第:"+str(i)+"个kepid: "+str(temp))
# print(my_array)
In [3]
#输出
df2=pd.DataFrame(pd.read_csv("C:\\Users\\96502\\kepler\\dr24_tce_500.csv"))
df2.insert(0, 'flag', my_array)
filterDf=df2.flag[df2['flag']=='F']
df2=df2.drop(filterDf.index)
df2 = df2.reset_index()
df2.index=df2.index+1
df2['rowid'] = df2.index
del df2['flag']
del df2['index']
print(df2)
df2.to_csv('outputkepler24.csv',index=False)
reference:https://blog.csdn.net/lilong117194/article/details/74537595