首页 > 解决方案 > 如何使用连续循环更改python中的目录?

问题描述

如何设置循环以连续更改目录?

import os

print(os .getcwd())
os.system('./gromacs.sh')

os.chdir('/home/abhi/screening/without_membrane/2_NS/MA_33_SCRENNING/KCATBH002/')
print(os.getcwd())
os.system ('python gromacs.py')

os.chdir('/home/abhi/screening/without_membrane/2_NS/MA_33_SCRENNING/KCATBH003/')
print(os.getcwd())
os.system('python gromacs.py')

os.chdir('/home/abhi/screening/without_membrane/2_NS/MA_33_SCRENNING/KCATBH004/')
print(os.getcwd())
os.system('python gromacs.py')

os.chdir('/home/abhi/screening/without_membrane/2_NS/MA_33_SCRENNING/KCATBH005/')
print(os.getcwd())
os.system('python gromacs.py')

os.chdir('/home/abhi/screening/without_membrane/2_NS/MA_33_SCRENNING/KCATBH006/')
print(os.getcwd())
os.system('python gromacs.py')

os.chdir('/home/abhi/screening/without_membrane/2_NS/MA_33_SCRENNING/KCATBH007/')
print(os.getcwd())
os.system('python gromacs.py')

os.chdir('/home/abhi/screening/without_membrane/2_NS/MA_33_SCRENNING/KCATBH008/')
print(os.getcwd())
os.system('python gromacs.py')

os.chdir('/home/abhi/screening/without_membrane/2_NS/MA_33_SCRENNING/KCATBH009/')
print(os.getcwd())
os.system('python gromacs.py')

标签: python-3.x

解决方案


使用 for 循环遍历您要经过的所有目录?

var directory = '/home/abhi/screening/without_membrane/2_NS/MA_33_SCRENNING/KCATBH00'
var numOfIterations = 10 #Arbitrary value

for x in range(1, numOfIterations):
    os.chdir(directory + str(x) + '/')
    print(os.getcwd())
    os.system('python gromacs.py')

推荐阅读