简体   繁体   中英

Loop only works once

. dd language identifier to highlight code

def function(foo):
    print(foo)

► put returns between paragraphs

► for linebreak add 2 spaces at end

italic or bold

► indent code by 4 spaces

► backtick escapes like _so_

► quote by placing > at start of line

If the for is causing you trouble (with the len and the range ), why not simply do:

for file in files:
    print file

Much simpler, much cleaner, and it may give you a better indication as to what exactly is wrong.

I suspect that you are altering the s variable (or potentially the files variable) later on in your code. Also, may I suggest using argparse ?

I removed the global assignment (besides making it python 3) and tested with a shorter test command python script.py -f traj1.gro,traj2.gro -i 1 -s 30 :

import sys
files = []
#global files

for i in sys.argv[1:]:
    if i == '-f':
        file = sys.argv[sys.argv.index('-f')+1] #input filenames after -f
        for j in file.split(','):
            files.append(j)

for s in range(len(files)):
    file = ''
    print(s)
    print(files[s])
    file = files[s]

It spitted out:

0
traj1.gro
1
traj2.gro

I would recommend not using system names like file and files for variables

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM