find find with file name as variable in glob

2024/11/15 0:44:29

I want to delete specific files from a list of csv files.

the file name is something like this: Storm.abc.ibtracs_all.v03r10.csv. where abc is different for each file.

I have a list of the abc part (namely tmp2) of the files that I want to delete.

my idea is to loop through the list above with the file name as variable and pass it to glob to get the file name that matches and then delete it

import globfor i in range(len(tmp2)):name=tmp2[i]files=glob.glob('*.name.*')os.remove(files)

I doesnt work obviously. please help with the treatment of glob.glob('.name.')

Answer

This should help.

import glob
import ospath = "YourPath"
for i in tmp2:                              #Iterate Your Listfiles=glob.glob('*.{}.*'.format(i))     #Check for filesfor file in files:               fPath = os.path.join(path, file)    #Form full path to fileos.remove(fPath)                    #Remove
https://en.xdnf.cn/q/119694.html

Related Q&A

Remove Special Chars from a TSV file using Regex

I have a File called "X.tsv" i want to remove special characters (including double spaces) (excluding . Single spaces Tabs / -) using regex before i export them to sub files in python I want…

How to inherit a python base class?

dir/||___ __init__.py||___ Base_class.py||___ Subclass.py__init__.py is empty(as mentioned here)/* Base_class.pyclass Employee:numOfEmployees = 0 # Pure class member, no need to overrideraiseAmount = 1…

Validation for int(input()) python

def is_digit(x):if type(x) == int:return Trueelse:return Falsedef main():shape_opt = input(Enter input >> )while not is_digit(shape_opt):shape_opt = input(Enter input >> )else:print(it work…

How to get data out of a def function in python [duplicate]

This question already has answers here:How do I get ("return") a result (output) from a function? How can I use the result later?(4 answers)Closed 1 year ago.Trying to simplify lots of repe…

Calculating RSI in Python

I am trying to calculate RSI on a dataframedf = pd.DataFrame({"Close": [100,101,102,103,104,105,106,105,103,102,103,104,103,105,106,107,108,106,105,107,109]})df["Change"] = df["…

Pandas groupwise percentage

How can I calculate a group-wise percentage in pandas?similar to Pandas: .groupby().size() and percentages or Pandas Very Simple Percent of total size from Group by I want to calculate the percentage…

How to deal with large json files (flattening it to tsv) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 3 years ago.Improve…

How can I find max number among numbers in this code?

class student(object):def student(self):self.name=input("enter name:")self.stno=int(input("enter stno:"))self.score=int(input("enter score:"))def dis(self):print("nam…

Assert data type of the values of a dict when they are in a list

How can I assert the values of my dict when they are in a list My_dict = {chr7: [127479365, 127480532], chr8: [127474697, 127475864], chr9: [127480532, 127481699]}The code to assert this assert all(isi…

Loading tiff images in fiftyone using ipynp

I am trying to load tiff images using fiftyone and python in ipynb notebook, but it just doesnt work. Anyone knows how to do it?