使用文件作者使用Python中的文件 我试图考虑通过目录树运行并识别特定的Excel文件的最佳方式,然后继续在大熊猫中操作它们。

使用文件作者使用Python中的文件 我试图考虑通过目录树运行并识别特定的Excel文件的最佳方式,然后继续在大熊猫中操作它们。,python,windows,file,operating-system,shutil,Python,Windows,File,Operating System,Shutil,我试图通过扫描文件名(数据)来识别我想要的文件,但我意识到,如果我能够识别文件作者的文件,效果会好得多。我怎样才能重做下面的例子,从搜索“数据”到搜索文件的作者 我在示例中添加了file.lower(),因为有些文件可能包含数据或文件名中的数据。如果有更好的方法做到这一点,如果有一个很好的资源来学习更多关于操作文件的内容,如我在文章中所描述的,我将非常高兴听到 import os import shutil for folderName, subfolders, filenames in os.

我试图通过扫描文件名(数据)来识别我想要的文件,但我意识到,如果我能够识别文件作者的文件,效果会好得多。我怎样才能重做下面的例子,从搜索“数据”到搜索文件的作者

我在示例中添加了file.lower(),因为有些文件可能包含数据或文件名中的数据。如果有更好的方法做到这一点,如果有一个很好的资源来学习更多关于操作文件的内容,如我在文章中所描述的,我将非常高兴听到

import os
import shutil
for folderName, subfolders, filenames in os.walk(r'dir\Documents'):

        for file in filenames:
                file.lower()
                if 'data' in file:
                        try: shutil.copy(os.path.join(folderName, file), 'C:\\dir\ALL DATA')
                        except: 
                                print(folderName, file)

这将在目录中搜索excel文件元数据中具有创建者名称的excel文件:

import os
import zipfile, lxml.etree
import shutil

def find_excel_files(directory, creator_name):
    for folderName, subfolders, filenames in os.walk(directory):
        for f in filenames:
            if f.endswith('.xlsx'):
                f_path = os.path.join(folderName, f)
                f_creators = get_xlsx_creators(f_path)
                if creator_name in f_creators:
                    # One of the creators of the excel file matches creator_name
                    # Do something like copy the file somewhere...
                    print('Found a match: {}'.format(f_path))



def get_xlsx_creators(xlsx_path):
    # open excel file (xlsx files are just zipfiles)
    zf = zipfile.ZipFile(xlsx_path)
    # use lxml to parse the xml file we are interested in
    doc = lxml.etree.fromstring(zf.read('docProps/core.xml'))
    # retrieve creator
    ns={'dc': 'http://purl.org/dc/elements/1.1/'}
    creators = doc.xpath('//dc:creator', namespaces=ns)
    return [c.text for c in creators]



find_excel_files(r'C:\Users\ayb\Desktop\tt', 'Tim')

get_xlsx_creators函数的代码取自此SO答案:

这将搜索目录中excel文件元数据中具有创建者名称的excel文件:

import os
import zipfile, lxml.etree
import shutil

def find_excel_files(directory, creator_name):
    for folderName, subfolders, filenames in os.walk(directory):
        for f in filenames:
            if f.endswith('.xlsx'):
                f_path = os.path.join(folderName, f)
                f_creators = get_xlsx_creators(f_path)
                if creator_name in f_creators:
                    # One of the creators of the excel file matches creator_name
                    # Do something like copy the file somewhere...
                    print('Found a match: {}'.format(f_path))



def get_xlsx_creators(xlsx_path):
    # open excel file (xlsx files are just zipfiles)
    zf = zipfile.ZipFile(xlsx_path)
    # use lxml to parse the xml file we are interested in
    doc = lxml.etree.fromstring(zf.read('docProps/core.xml'))
    # retrieve creator
    ns={'dc': 'http://purl.org/dc/elements/1.1/'}
    creators = doc.xpath('//dc:creator', namespaces=ns)
    return [c.text for c in creators]



find_excel_files(r'C:\Users\ayb\Desktop\tt', 'Tim')

get_xlsx_creators函数的代码取自此,因此答案:

lower()不会在适当的位置更改字符串,而是返回字符串。因此,您应该执行类似于lower\u file=file.lower()的操作。此外,是否所有excel文件都在文件本身中指定了作者?在上面的示例中,您可能希望
如果“数据”if file.lower():
lower()不更改字符串,而是返回字符串。因此,您应该执行类似lower_file=file.lower()的操作。此外,是否所有excel文件都在文件本身中指定了作者?在上面的示例中,您可能希望
if“data”if file.lower():
,除非OP表示文档元数据中的
作者
?True,但根据我的经验,这个领域通常不是很可靠。它在创建文件时设置。如果其他人修改原始数据并保存,则该元数据不会得到更新。我认为OP在Windows上(C:\\dir\ALL DATA)。pwd在Windows上可用吗?@LeopoldVonBuschLight,pwd在Windows中不可用。此外,Python在Windows上的自定义
stat
实现没有定义
st\u uid
。Windows上的等效代码将通过PyWin32的Win32安全模块或低级CType调用
GetNamedSecurityInfo
LookupAccountSid
。但我认为OP想要元数据作者。除非OP指的是文档元数据中的
作者
,是的,但根据我的经验,该字段通常不太可靠。它在创建文件时设置。如果其他人修改原始数据并保存,则该元数据不会得到更新。我认为OP在Windows上(C:\\dir\ALL DATA)。pwd在Windows上可用吗?@LeopoldVonBuschLight,pwd在Windows中不可用。此外,Python在Windows上的自定义
stat
实现没有定义
st\u uid
。Windows上的等效代码将通过PyWin32的Win32安全模块或低级CType调用
GetNamedSecurityInfo
LookupAccountSid
。但我认为OP想要元数据作者。