How to delete text file

how to delete a text file programmatically in ignition by using event client scripts?
and what is script for this?
please anyone give me the solution for this.

short example:

import os os.remove("c:\\temp\\readme.txt")

is this for linux environment?

the specific example script was for a Windows file. Just put whatever path to the file inside the quotes and you should be good to go. :slight_smile:

thank you it is worked under linux also

Could this also be applied to a file type? Lets say I want to delete any csv file in a folder. Could I use *.csv instead of a specific file? I will not know what the name of the file is, just that its a csv file.

Yes this is for any type of file.

Tried this and get an error back:

	import os

	filepath = 'C:\\Recipes\\BT548647\\*.csv'
	os.remove(filepath)

image

Try with the edited code. I think the double slashes are giving you issues…

FWIW, not certain if os.remove accepts wildcards natively. Seems like you could make use of glob though.

import os
import glob

files = glob.glob("C:\\Recipes\\BT548647\\*.csv")
for f in files:
	os.remove(f)

https://docs.python.org/2.7/library/glob.html

3 Likes

This did work. Thanks. For the function script I am already using the glob library so that works too. I also got the following to work:

import os
	from os import listdir
	my_path = 'C:\\Recipes\\BT548647\\'
	
	for file_name in listdir(my_path):
		if file_name.endswith('.csv'):
			os.remove(my_path + file_name)

Think I will use your route though. cleaner and easier.

still not sure why my initial script did not work. it seems as if it would. maybe because the path and filename have to be separate?

1 Like