【问题标题】:Can't Take in Multiple Text File无法接收多个文本文件
【发布时间】:2013-10-29 08:29:35
【问题描述】:

您好,这是我遇到的问题。以下程序不允许我接收多个平面外数据,但它允许根据call_Controller 中的函数接收多个平面内数据。程序有问题吗?

you can try by creating this 4 text file in a folder.
run the program and press Clt to take in both hello.txt and bye.txt
follow by hello1.txt and bye1.txt and you will know the error 
somebody please help me with this problem..
hello.txt | hello1.txt
bye.txt   | bye1.txt

错误信息:

Traceback (most recent call last):
File "C:\ProjectMAPG\TRYPYTHON\src\OldPythonTry.py", line 100, in <module>
Name = call_Controller(file_path_IP, InputfilesListOoplane)
File "C:\ProjectMAPG\TRYPYTHON\src\OldPythonTry.py", line 67, in call_Controller
with open(InputfilesListOoplane) as f1:
IOError: [Errno 22] invalid mode ('r') or filename: u'C:/Users/zhenhui/Desktop/bye1.txt C:/Users/zhenhui/Desktop/hello1.txt'

提取文件名的函数:

def extractFilename(FileNameOnly):
    stripText = string.split(FileNameOnly, " ")
    FileName = stripText[0]   
    return (FileName)
    pass

我认为问题就在这里。由于外机不允许我进入多个文件夹:

def call_Controller(file_path_Inplane, InputfilesListOoplane):
    FileNameOnlyIP = splitext(basename(file_path_Inplane))[0]
    Name = extractFilename(FileNameOnlyIP)
    #Extract raw inplane
    with open(file_path_Inplane) as f:
        f.readline()      
    #Extract raw outplane
    with open(InputfilesListOoplane) as f1:
        f1.readline()
    return Name

程序开始:

if __name__ == '__main__': #start of program
    win = Tk.Tk()  #Open up connection and declare button and label  
    master=win
    initial_dir = "C:\VSM"

    #Create Directory if its not there
    try:
            if not os.path.exists(newDirRH[0]):os.makedirs(newDirRH)
    except: pass

    #Open to select multiple files for files
    file_path_Inplane= tkFileDialog.askopenfilename(initialdir=initial_dir, title="Select VSM Files", multiple =1)
    if file_path_Inplane != "": pass
    else:
        print "You didn't open anything!"
    InputfilesListInplane =  win.tk.splitlist(file_path_Inplane)

    #Open to select multiple files for outplane 
    file_path_Ooplane = tkFileDialog.askopenfilename(initialdir=initial_dir, title="Select Out of Plane Files", multiple = 1)
    if file_path_Ooplane != "": 
        pass
    else:
        print "You didn't open anything!"
    InputfilesListOoplane =  file_path_Ooplane#master.tk.splitlist(file_path_Ooplane)


    for file_path_IP in InputfilesListInplane:
        Name = call_Controller(file_path_IP, InputfilesListOoplane)

    print "Done " + Name

【问题讨论】:

    标签: python file text extract


    【解决方案1】:

    我不确定“in multiple outplane data”可能是什么,但错误消息解释得很清楚:

    'C:/Users/zhenhui/Desktop/bye1.txt C:/Users/zhenhui/Desktop/hello1.txt'
    

    不是有效的文件名。对我来说,这看起来像是两个文件名,您在它们之间附加了一个空格。

    当您打开文件时,Python 和操作系统都不会尝试读取您的想法。每个操作使用一个文件名。

    【讨论】:

    • 这只是一个例子。我必须为我的项目获取多个数据。有没有办法拆分 InputfilesListOoplane
    • 这个变量的类型是什么?
    【解决方案2】:

    这里有问题:

    IOError: [Errno 22] invalid mode ('r') or filename: u'C:/Users/zhenhui/Desktop/bye1.txt C:/Users/zhenhui/Desktop/hello1.txt'
    

    检查您的路径的正确交货地点。 下一个潜在的问题在这里initial_dir = "C:\VSM"。必须initial_dir = "C:\\VSM" 或initial_dir = "C:/VSM"。此外使用来自os.pathmodule的路径相关函数。

    【讨论】:

      猜你喜欢
      • 2021-03-28
      • 2018-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-12
      相关资源
      最近更新 更多