У меня есть список, содержащий каталоги и файлы, я хочу сохранить только файлы и написать следующий код для его фильтрации. Однако я обнаружил, что некоторые записи все еще находятся в этом списке, например 'wangshx / ,
' '`.
Из этого результата я обнаружил, что в предложении if
может быть что-то не так. Кто-нибудь может указать, где проблема?
In [7]: filelist = ['/tmp/test2.pbs', '/tmp/test.pbs', '/public/home/
...: wangshx:', ' ', 'correct_order.txt', 'download/', 'filepaths.
...: RData', 'lib/', 'Log.out', 'ncbi_error_report.xml', 'new_hg19
...: .1.bt2', 'new_hg19.2.bt2', 'new_hg19.3.bt2', 'new_hg19.4.bt2'
...: , 'new_hg19.fa', 'new_hg19.rev.1.bt2', 'new_hg19.rev.2.bt2',
...: 'perl5/', 'practice/', 'repnames_nfragments.txt', 'soft/', 's
...: ongmf/', 'sort.pbs', 'test.pbs', 'test.pbs.o1167575', 'test.p
...: bs.o1167590', 'tmp/', 'wangshx/', 'workspace/', 'wt/', 'wx/',
...: '']
In [8]: len(filelist)
Out[8]: 32
In [9]: for f in filelist:
...: print(f)
...:
...:
/tmp/test2.pbs
/tmp/test.pbs
/public/home/wangshx:
correct_order.txt
download/
filepaths.RData
lib/
Log.out
ncbi_error_report.xml
new_hg19.1.bt2
new_hg19.2.bt2
new_hg19.3.bt2
new_hg19.4.bt2
new_hg19.fa
new_hg19.rev.1.bt2
new_hg19.rev.2.bt2
perl5/
practice/
repnames_nfragments.txt
soft/
songmf/
sort.pbs
test.pbs
test.pbs.o1167575
test.pbs.o1167590
tmp/
wangshx/
workspace/
wt/
wx/
In [10]: for f in filelist:
...: print(f)
...: if f[-1]=='/' or f[-1]==':' or f=='' or f==' ':
...: print("=> Should remove " + f)
...: filelist.remove(f)
...:
/tmp/test2.pbs
/tmp/test.pbs
/public/home/wangshx:
=> Should remove /public/home/wangshx:
correct_order.txt
download/
=> Should remove download/
lib/
=> Should remove lib/
ncbi_error_report.xml
new_hg19.1.bt2
new_hg19.2.bt2
new_hg19.3.bt2
new_hg19.4.bt2
new_hg19.fa
new_hg19.rev.1.bt2
new_hg19.rev.2.bt2
perl5/
=> Should remove perl5/
repnames_nfragments.txt
soft/
=> Should remove soft/
sort.pbs
test.pbs
test.pbs.o1167575
test.pbs.o1167590
tmp/
=> Should remove tmp/
workspace/
=> Should remove workspace/
wx/
=> Should remove wx/
In [11]: filelist
Out[11]:
['/tmp/test2.pbs',
'/tmp/test.pbs',
' ',
'correct_order.txt',
'filepaths.RData',
'Log.out',
'ncbi_error_report.xml',
'new_hg19.1.bt2',
'new_hg19.2.bt2',
'new_hg19.3.bt2',
'new_hg19.4.bt2',
'new_hg19.fa',
'new_hg19.rev.1.bt2',
'new_hg19.rev.2.bt2',
'practice/',
'repnames_nfragments.txt',
'songmf/',
'sort.pbs',
'test.pbs',
'test.pbs.o1167575',
'test.pbs.o1167590',
'wangshx/',
'wt/',
'']
Best,
Shixiang