I have a list containing directories and files, I want to keep only files and write the following code to filter it. However, I found some records are still in this list, like 'wangshx/,' '`.
From this result, I found there may be something wrong about the if sentence. Can anyone point out where the problem is?
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
 
     
    