Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 Python—将所有文件名从一个程序(dir.py)导入到另一个程序(pj.py)。但目录中存储的文件名会列出两次_Python 2.7 - Fatal编程技术网

Python 2.7 Python—将所有文件名从一个程序(dir.py)导入到另一个程序(pj.py)。但目录中存储的文件名会列出两次

Python 2.7 Python—将所有文件名从一个程序(dir.py)导入到另一个程序(pj.py)。但目录中存储的文件名会列出两次,python-2.7,Python 2.7,将所有文件名从一个程序dir.py导入另一个程序pj.py。但是从dir.py传递到pj.py的文件名是两次而不是一次 这是dir.py程序- #!/usr/bin/python import os root = 'C:/Users/Desktop/Files/test' ##THE PATH IS WHERE ALL EXCEL DATASHEETS ARE STORED. # Traverse directory, and list files for files in os.listdi

将所有文件名从一个程序dir.py导入另一个程序pj.py。但是从dir.py传递到pj.py的文件名是两次而不是一次

这是dir.py程序-

#!/usr/bin/python
import os
root = 'C:/Users/Desktop/Files/test' ##THE PATH IS WHERE ALL EXCEL DATASHEETS ARE STORED. 
# Traverse directory, and list files
for files in os.listdir(root):
    #Call the main python program
    file_location = os.path.join(root, files)
    print files
    execfile('C:/Users/Desktop/pj.py')

#THIS PATH REFERS TO THE DIRECTORY WHERE THE PYTHON PROGRAM pj.py is going to manipulate the datasheet. It takes the file name from dir.py as a parameter.
import xlrd
import re
from dir import file_location

#The file location is passed as a argument from first program. Worksheet is open
workbook = xlrd.open_workbook(file_location)
现在,在pj.py程序中-

#!/usr/bin/python
import os
root = 'C:/Users/Desktop/Files/test' ##THE PATH IS WHERE ALL EXCEL DATASHEETS ARE STORED. 
# Traverse directory, and list files
for files in os.listdir(root):
    #Call the main python program
    file_location = os.path.join(root, files)
    print files
    execfile('C:/Users/Desktop/pj.py')

#THIS PATH REFERS TO THE DIRECTORY WHERE THE PYTHON PROGRAM pj.py is going to manipulate the datasheet. It takes the file name from dir.py as a parameter.
import xlrd
import re
from dir import file_location

#The file location is passed as a argument from first program. Worksheet is open
workbook = xlrd.open_workbook(file_location)
当我打印它时,它会显示以下内容:

['Disambiguation_Version 1t_Bo Zheng.xlsx', 'Disambiguation_Version 1t_Jun Xi.xlsx', 'Disambiguation_Version 1_Samantha_26nov2014.xlsx', 'Disambiguation_Version 1_Xin Hua_entiredata.xlsx', 'Disambiguation_Version 2o_Maximus.xlsx', 'Disambiguation_Version 2o_theresa.xlsx', 'Disambiguation_Version 2t_Cheng Ai.xlsx', 'Disambiguation_Version 2_Summer_entiredata.xlsx', 'Disambiguation_Version 2_Tricia.xlsx', 'Disambiguation_Version 3_Emily_entiredataset.xlsx', 'test']

['Disambiguation_Version 1_Samantha_26nov2014.xlsx', 'Disambiguation_Version 1_Xin Hua_entiredata.xlsx', 'Disambiguation_Version 2t_Cheng Ai.xlsx']
它给了我两个数组而不是一个,因此我的文件被处理了两次


请提供一些关于我哪里出错的信息

您有几个问题:

您正在每个循环迭代中打印列表。 在每个循环迭代中调用pj.py文件。 您传递的不是文件的完整路径,而是文件名。 您已将文件命名为dir.py,这是一个内置函数的名称。 首先,您应该使用glob,在文件中创建一个列出目录的方法,然后在其他程序中调用此方法,而不是使用execfile:

在一个文件中,我们将其称为list_excel.py,您可以获得如下代码:

import glob
import os

def get_excel_files(directory=None):
    for filename in glob.iglob(os.path.join(directory, '*.xls?')):
        yield filename
在pj.py中,您可以导入此函数,确保list_excel.py和pj.py位于同一目录中:

from list_excel import get_excel_files

root = 'C:/Users/Desktop/Files/test'

for file_location in get_excel_files(root):
    workbook = xlrd.open_workbook(file_location)

当我们想威胁某人时,我们用大写字母写。谢谢你的回答。我在第一个程序中执行文件的原因是我不想在第二个程序中做任何更改。是否可以保持程序不变,只更改调用excel文件的第一个程序。有10个文件。每个文件都必须进行操作。我想将文件名作为变量传递给执行所有操作的pj.py程序。敬请告知。这完全取决于pj.py如何期望文件参数。如果它期望从标准输入中得到它们,那么您可以使用excel工作表的完整路径作为参数来调用它。您还可以执行其他杂技,如导入模块,然后屏蔽名称;但当你能接触到源头时;简单地修改pj.py将是最简单的解决方案。