在Python中查找目录中的文件

时间:2021-01-24 22:10:21

I've been doing some scripting where I need to access the os to name images (saving every subsequent zoom of the Mandelbrot set upon clicking) by counting all of the current files in the directory and then using %s to name them in the string after calling the below function and then adding an option to delete them all

我一直在做一些脚本,我需要通过计算目录中的所有当前文件,然后使用%s在字符串中命名它们来访问操作系统以命名图像(在点击时保存Mandelbrot集的每个后续缩放)在调用下面的函数然后添加一个选项来删除它们全部

I realize the below will always grab the absolute path of the file but assuming we're always in the same directory is there not a simplified version to grab the current working directory

我意识到下面将总是抓住文件的绝对路径,但假设我们总是在同一个目录中,没有简化版本来获取当前的工作目录

def count_files(self):
     count = 0
     for files in os.listdir(os.path.abspath(__file__))):
         if files.endswith(someext):
             count += 1
     return count

def delete_files(self):
     for files in os.listdir(os.path.abspath(__file__))):
         if files.endswith(.someext):
             os.remove(files)

3 个解决方案

#1


1  

Since you're doing the .endswith thing, I think the glob module might be of some interest.

既然你正在做.endswith的事情,我认为glob模块可能会引起一些兴趣。

The following prints all files in the current working directory with the extension .py. Not only that, it returns only the filename, not the path, as you said you wanted:

以下打印当前工作目录中扩展名为.py的所有文件。不仅如此,它只返回文件名,而不是路径,如您所说:

import glob

for fn in glob.glob('*.py'): print(fn)

Output:

输出:

temp1.py
temp2.py
temp3.py
_clean.py

Edit: re-reading your question, I'm unsure of what you were really asking. If you wanted an easier way to get the current working directory than

编辑:重新阅读你的问题,我不确定你真正在问什么。如果您想要一种更简单的方法来获取当前工作目录

os.path.abspath(__file__) 

Then yes, os.getcwd()

然后是的,os.getcwd()

But os.getcwd() will change if you change the working directory in your script (e.g. via os.chdir(), whereas your method will not.

但是如果你改变脚本中的工作目录(例如通过os.chdir()),os.getcwd()将会改变,而你的方法则不会。

#2


0  

Using antipathy* it gets a little easier:

使用反感*它变得容易一些:

from antipathy import Path

def count_files(pattern):
    return len(Path(__file__).glob(pattern))

def deletet_files(pattern):
    Path(__file__).unlink(pattern)

*Disclosure: I'm the author of antipathy.

*披露:我是反感的作者。

#3


0  

You can use os.path.dirname(path) to get the parent directory of the thing path points to.

您可以使用os.path.dirname(path)来获取指向事物路径的父目录。

def count_files(self):
 count = 0
 for files in os.listdir(os.path.dirname(os.path.abspath(__file__)))):
     if files.endswith(someext):
         count += 1
 return count

#1


1  

Since you're doing the .endswith thing, I think the glob module might be of some interest.

既然你正在做.endswith的事情,我认为glob模块可能会引起一些兴趣。

The following prints all files in the current working directory with the extension .py. Not only that, it returns only the filename, not the path, as you said you wanted:

以下打印当前工作目录中扩展名为.py的所有文件。不仅如此,它只返回文件名,而不是路径,如您所说:

import glob

for fn in glob.glob('*.py'): print(fn)

Output:

输出:

temp1.py
temp2.py
temp3.py
_clean.py

Edit: re-reading your question, I'm unsure of what you were really asking. If you wanted an easier way to get the current working directory than

编辑:重新阅读你的问题,我不确定你真正在问什么。如果您想要一种更简单的方法来获取当前工作目录

os.path.abspath(__file__) 

Then yes, os.getcwd()

然后是的,os.getcwd()

But os.getcwd() will change if you change the working directory in your script (e.g. via os.chdir(), whereas your method will not.

但是如果你改变脚本中的工作目录(例如通过os.chdir()),os.getcwd()将会改变,而你的方法则不会。

#2


0  

Using antipathy* it gets a little easier:

使用反感*它变得容易一些:

from antipathy import Path

def count_files(pattern):
    return len(Path(__file__).glob(pattern))

def deletet_files(pattern):
    Path(__file__).unlink(pattern)

*Disclosure: I'm the author of antipathy.

*披露:我是反感的作者。

#3


0  

You can use os.path.dirname(path) to get the parent directory of the thing path points to.

您可以使用os.path.dirname(path)来获取指向事物路径的父目录。

def count_files(self):
 count = 0
 for files in os.listdir(os.path.dirname(os.path.abspath(__file__)))):
     if files.endswith(someext):
         count += 1
 return count