The following code prints a list of directories that all happen to contain a 3 letter code, Example:
//Server/Jobs/2016\\AAM - 'areallylongfilename'/
//Server/Jobs/2016\\CLM - 'areallylongfilename'/
//Server/Jobs/2016\\COO - 'areallylongfilename'/
import os
basepath = '//Server/Jobs/2016'
for fname in os.listdir(basepath):
path = os.path.join(basepath, fname)
if os.path.isdir(path):
print(path)
How can I get one directory from the list based on the 3 letter code?
import os
basepath = '//Server/Jobs/2016'
asked_name = 'COO'
if len(asked_name) != 3:
print "Expected 3 letter code, got:", asked_name
else:
for fname in os.listdir(basepath):
path = os.path.join(basepath, fname)
if os.path.isdir(path):
if fname == asked_name:
print(path)
Suppose that you want to scan the "d:" disk, you can code as:
import os dir="d:\\" for root,dirs,files in os.walk(dir): for a_dir in dirs: if ("Server" in a_dir) and ("Jobs" in a_dir) and ("2016" in a_dir): print os.path.join(root,a_dir)
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.