Python: 循环一定的时间顺序不同的进程

Python: Looping a certain amount of time in order different process

我制作了一个脚本,假设使用 Tkinter 允许选择和加载文件并将它们的内容存储在不同的对象中,然后处理这些文档中的每一个。 我想让脚本只能处理由问题确定的一定数量的文档(该值存储在 "File_number" 下) 例如:如果在问题 "how many files do you want to compare?" 用户输入 3 我希望 tkinter openfile window 只要求 3 个文件然后继续

我正在使用如下的 If Else 语句 但它似乎效果不佳,而且代码真的不是 pythonic 的。 是否有 better/shorter 方法来执行相同的操作?

谢谢

我的脚本是这样的

import pandas as pd
from pandas import *
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
import seaborn as sns
import pylab
import pandas.io.data
import os
import Tkinter
from Tkinter import *
import tkFileDialog
import tkSimpleDialog
from tkFileDialog import askopenfilename
import sys

# Set up GUI
root = Tkinter.Tk(); root.withdraw()

# Prompt for user info
File_number = tkSimpleDialog.askinteger("File number", "How many files do you want to compare?")


# Prompt for file explorer
# Also extract the file_name

process_a = 0

if process_a = File_number:
    break
else:
    process_a = process_a + 1
    fileloc1 = tkFileDialog.askopenfilename(parent=root, title='Choose file 1')
    fileloc1_name_clean = os.path.splitext(fileloc1)[0]
    fileloc1_name = os.path.basename(fileloc1_name_clean)

if process_a = File_number:
    break
else:
    process_a = process_a + 1
    fileloc2 = tkFileDialog.askopenfilename(parent=root, title='Choose file 2')
    fileloc2_name_clean = os.path.splitext(fileloc2)[0]
    fileloc2_name = os.path.basename(fileloc2_name_clean)

if process_a = File_number:
    break
else:
    process_a = process_a + 1
    fileloc3 = tkFileDialog.askopenfilename(parent=root, title='Choose file 3')
    fileloc3_name_clean = os.path.splitext(fileloc3)[0]
    fileloc3_name = os.path.basename(fileloc3_name_clean)
编辑 1
The next part of my script is:

dfa_1 = pd.read_csv(fileloc1, delimiter='\t')
dfa_nodupli = dfa_1.drop_duplicates(cols='N', take_last=False)    
dfa_nodu_2pep = dfa_nodupli[(dfa_nodupli['Peptides(95%)'] > 1)]   
dfa_nodu_2pep = dfa_nodu_2pep[~dfa_nodu_2pep['Name'].str.contains('Keratin')]
dfa_nodu_2pep.to_csv(fileloc1_name + ".csv")



dfb_1 = pd.read_csv(fileloc2, delimiter='\t')
dfb_nodupli = dfb_1.drop_duplicates(cols='N', take_last=False)    
dfb_nodu_2pep = dfb_nodupli[(dfb_nodupli['Peptides(95%)'] > 1)]   
dfb_nodu_2pep = dfb_nodu_2pep[~dfb_nodu_2pep['Name'].str.contains('Keratin')]
dfb_nodu_2pep.to_csv(fileloc2_name + ".csv")

我修改了您的代码,使其以您想要的方式运行(我希望如此)。

import Tkinter
import tkFileDialog
import tkSimpleDialog
from tkFileDialog import askopenfilename
import os

# Set up GUI


def main():

    root = Tkinter.Tk(); 
    root.withdraw()

    # Prompt for user info
    File_number = tkSimpleDialog.askinteger("File number", 
                                            "How many files do you want to compare?")

    if not File_number:
        return

    user_fiels = []


    max_file_no = int(File_number)
    current_file = 1;

    while(current_file <= max_file_no):

        fileloc = tkFileDialog.askopenfilename(parent=root, title='Choose file {}'.format(current_file))

        if not fileloc:
            continue

        fileloc_name_clean = os.path.splitext(fileloc)[0]
        fileloc_name = os.path.basename(fileloc_name_clean)

        user_fiels.append([fileloc, fileloc_name_clean, fileloc_name])

        current_file += 1

        #print(fileloc_name_clean, fileloc_name)

    print(user_fiels) 

main()

我使用 while 循环多次获取文件路径。