【发布时间】:2015-05-22 03:46:22
【问题描述】:
我一直在编写一些脚本,我需要访问操作系统来命名图像(单击时保存 Mandelbrot 集的每个后续缩放),方法是计算目录中的所有当前文件,然后使用 %s 来命名它们在调用以下函数后在字符串中添加一个选项以将它们全部删除
我意识到下面将始终获取文件的绝对路径,但假设我们始终在同一个目录中,是否没有简化版本来获取当前工作目录
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)
【问题讨论】:
标签: python python-3.x data-structures operating-system filesystems