Get and display all files under the current directory.
import os
import glob
rootpath='.'
for root, dirs, files in os.walk(rootpath):
for file_ in files:
file_path=os.path.join(root,file_)
print file_path
Get and display all files under the current directory.
import os
import glob
rootpath='.'
for root, dirs, files in os.walk(rootpath):
for file_ in files:
file_path=os.path.join(root,file_).split('/')[-1]
print file_path
Recursively get all .tab files under the current directory and display them.
import os
import glob
path='.'
for root, dirs, files in os.walk(path):
for dir_ in dirs:
dir_path=os.path.join(root,dir_)
for table in glob.glob(dir_path+'/*.tab'):
print table
>>> num=1
>>> "{0,04d}".format(num)
'0001'
>>> num=1
>>> "{0,4d}".format(num)
' 1'
>>> char='1'
>>> char.zfill(4)
'0001'
>>> import re
>>> hoge='1257cfnewoaij2203'
>>> suuji=re.findall("(\d+)",hoge)
>>> print suuji
'1257' '2203'
It is assumed that each column contains a physical quantity. If the columns are separated by whitespace.
When a mixture of number strings and character strings
for line in open('test.tab', 'r'):
itemList = line[:-1].split()
print itemList
Split (',')
if separated by,, split ('/ t')
if separated by tabs.
>>> import numpy as np
>>> data=np.loadtxt('input.txt').T
When a mixture of number strings and character strings
f=open('output.txt','w')
f.write(moji+'\n')
f.close()
>>> np.savetxt('output.txt',data.T)
Recommended Posts