Python脚本可在目录中搜索某些文件类型,然后将其内容附加在一起 [英] Python script to search directory for certain file type then append their contents together
问题描述
我正在尝试制作一个python脚本,该脚本将在目录中搜索所有以特定文件扩展名.math结尾的文件(基本上是其中包含数字的文本文件)。
I am trying to make a python script that will search a directory for all files that end with a specific file extension called .math (basically a text file with numbers inside of them).
我将如何处理?
想象我有hello.math,hello2.math和goodbye.math文件
Imagine I have files hello.math, hello2.math, and goodbye.math
让我们说hello.math有10个,hello2.math有3个,goodbye.math有0个。
Lets say hello.math has 10, hello2.math has 3 and goodbye.math has 0.
我希望脚本能够在当前目录中搜索所有.math文件,然后对其进行汇总。
i would want the script to search for all of the .math files in the current directory then sum them up.
推荐答案
所以不确定是否要递归在目录结构中搜索,或仅在一个目录中列出文件。
So not sure if you want to recursively search through a directory structure, or just list files in one directory. This should get you going.
import os
MATH_FILES = list()
def single_dir(name)
global MATH_FILES
names = [os.path.join(name, item) for item in os.listdir(name)]
for name in names:
if ".math" in name and os.path.isfile(name):
print("%s has .math in the name" % name)
MATH_FILES.append(name)
def recursive(name):
global MATH_FILES
if ".math" in name and os.path.isfile(name):
print("%s has .math in the name" % name)
MATH_FILES.append(name)
try:
names = [os.path.join(name, item) for item in os.listdir(name)]
for name in names:
recurse(name)
except:
# Can't listdir() a file, so we expect this to except at the end of the tree.
pass
def sum_file_contents():
value = int()
for file in MATH_FILES:
value += int(open(file, "r").read().strip())
return value
if __name__ == "__main__":
# Choose implemntation here
# recursive("<insert base path here>")
# single_dir("<insert base path here>")
print(sum_file_contents())
所以有些事情……首先,在条件名称if中,您需要选择single_dir或递归函数,并指定路径入口点(例如/ dir或C:\dir或其他)。如果文件中除了int以外的任何内容,sum_file_contents都会失败。虽然您的示例说明了您要寻找的内容。
So some things.... Firstly in the if name conditional you need to choose either the single_dir or recursive function, and specify a path entry point (like /dir or C:\dir or whatever). The sum_file_contents will fail if there is anything other than an int in the file. Though your example states that is what you're looking for.
总而言之,如果您在提出问题之前先尝试一下,那就太好了。我认为整个社区不是在为您编写代码,而是在遇到困难时为您提供帮助。这是一个非常简单的问题。如果您在google中找到目录中的文件,我敢打赌,实际上有数十种答案,这至少可以为您提供代码示例的基础。然后您可以说我可以找到我的.math文件,我该如何对它们进行汇总?您可能也可以找到答案。请注意,我并不是想对本段不礼貌。
In conclusion, it would be good if you gave it a try before asking the question. I think the community at large isn't here to write code for you, but to help you if you get stuck. This is a super simple problem. if you google find files in a directory I bet there are literally dozens of answers, and that would at least give you a base of a code example. You could then say I can find my .math files like this, how do I sum them? You could probably find that answer simply as well. Please note I'm not trying to be rude with this paragraph.
注意:我没有测试这段代码
Note: I did not test this code
这篇关于Python脚本可在目录中搜索某些文件类型,然后将其内容附加在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!