想在python中找到扩展的解决方案吗
我是python新手,在这方面遇到了一些问题。 我想用python编写一个脚本,它包含两个输入: 1.目录路径 2.要删除的文件扩展名 如果用户键入要删除的路径和扩展名,那么我希望用户键入的文件扩展名保存在列表中,当解释器读取文件时,它会自动删除扩展名文件 代码如下:想在python中找到扩展的解决方案吗,python,Python,我是python新手,在这方面遇到了一些问题。 我想用python编写一个脚本,它包含两个输入: 1.目录路径 2.要删除的文件扩展名 如果用户键入要删除的路径和扩展名,那么我希望用户键入的文件扩展名保存在列表中,当解释器读取文件时,它会自动删除扩展名文件 代码如下: import os Ext_list = [] path=raw_input('Enter the path for Scanning : ') while True: x = raw_input('Enter the e
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)
使用这个,当然你必须修改它
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)
你尝试过什么吗?是的,由于信誉问题,无法发布图像粘贴代码,在其上粘贴代码,然后按ctrl+k进行修改,以便stackoverflow可以理解它是代码。我用SolutionInalReady回答,在此代码之前尝试过你的解决方案,但无法获得逻辑。此外,star将告诉python获取所有文件。如果您只有星号,那么它将获得与扩展无关的所有文件