I am trying to read some file which is inside some specific directory like. I am trying to use python2.7+ version. Here is my requirements like:
Find directory starts with B1234 (here 1234 is nr) inside output folder if directory exists then goto directories starting with only TEST_ read only file endswith YYYY.txt but that can reside inside subdirectory (name is not important here) I am trying to make following code working but in vain
for root, dirs, files in os.walk('output'):
for file in files:
if file.startswith('test') and file.endswith('_YYYY.txt'):
try:
f = open(os.path.abspath(os.path.join(root,file)) , 'r')
except:
print 'oops'
Problem is here I can find all desired files but also from unwanted directories.. i would like to use like that
for dir in dirList:
if dir startswith(B1234)
for testdir in os.listdir(dir)
if dir.startswiht(TEST_)
goto dir
search for file
Any help will be appreciable..please askif you need more info:
I think you need to make the change here :
for root, dirs, files in os.walk('output'):
for dir in dirs:
if dir.startswith('B1234') is False:
continue
for r, drs, fls in os.walk(dir):
# now write your code here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.