想在python中找到扩展的解决方案吗

2024-09-28 21:25:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python新手,在这方面遇到了一些问题。 我想用python编写一个脚本,它包含两个输入: 1.目录的路径 2.要删除的文件扩展名

如果用户键入要删除的路径和扩展名,那么我希望用户键入的文件扩展名保存在列表中,当解释器读取文件时,它会自动删除扩展名文件

代码如下:

import os
Ext_list = []
path=raw_input('Enter the path for Scanning : ')
while True:
    x = raw_input('Enter the extension: ')
    if x == ' ':
        break
    Ext_list.append(x)

for (path, subdirs, filesnames) in os.walk(path):
    if filesnames is (x):
        os.remove(filesnames)

Tags: 文件thepath用户路径forinputraw
1条回答
网友
1楼 · 发布于 2024-09-28 21:25:08

使用这个,当然你必须修改它

glob.glob(os.path.join('.', '*.path*'))

它将输出具有该扩展名的文件数组。然后呢

input = str(raw_input("Please enter file extention "))
files = glob.glob(os.path.join('.', '*.'+input.))
for File in files:
    os.remove(File)

相关问题 更多 >