Deleting a root file using gSystem

Dear co-rooters,

I am trying to perform a rather simple task.
A function accepts a TString as an argument and a boolan.
If the bool is true, then the file should be deleted.

This might seem strange but at some point later in the code a new root file is created. So I don’t know beforehand if I will use RECREATE of UPDATE as an option. So I decided to use UPDATE and if the bool is TRUE (i.e. the user doesn’t want to write again in this file) I delete the file using gSystem->Exec();
A simple version of the code can be found below


int Deltat(int run, TString filename_out, bool new_rootFile){

    if(new_rootFile){
	    cout << "Deleting file " << filename_out << endl;
	    gSystem->Exec(TString::Format("rm %s", filename_out));
    }

    TFile fout(filename_out, "UPDATE");
    cout << "Saving file " << fout.GetName() << endl;
    fout.Close();

   return run;

}

The thing is that when executing the code I get the following

root [7] Deltat(1, "Moore_1_Cut_PKUP_200_5000bpd_RF.root", 1)
Deleting file Moore_1_Cut_PKUP_200_5000bpd_RF.root
rm: cannot remove `P\3145P\372\177': No such file or directory

I have to note that the file indeed exists

$ ls Moore_1_Cut_PKUP_200_5000bpd_RF.root
Moore_1_Cut_PKUP_200_5000bpd_RF.root

Any idea on what’s that and how to fix it?

Thanks in advance!

TString::Format("rm -f %s", filename_out.Data())

Hi,

the platform independent way ROOT provides to delete files is TSystem::Unlink.

Cheers,
D

1 Like

So this can delete a file outside ROOT scope?

Hi,

can you elaborate? I do not understand what you mean by “ROOT scope”. If you mean something which is a root file, yes, you can delete any file.

Cheers,
D

I mean that when running root you can’t “communicate” with the shell.
But root provides gSystem->Exec("<shell command>") as a “communication” way.

So to delete a file I would do gSystem->Exec("rm filename");

What you are suggesting though is nice!
So to delete the file one would simply do Unlink("file.root"); right?
But this only works for root files?

If you are running on Unix like system that will work. What Danilo suggested you is working on all systems for all kind of files. But assuming you are running on an Unix like system you can do something like:

void delete_a_file() {
   gSystem->Exec("ls file_to_be_deleted");
   gSystem->Exec("touch file_to_be_deleted");
   gSystem->Exec("ls file_to_be_deleted");
   gSystem->Exec("rm file_to_be_deleted");
   gSystem->Exec("ls file_to_be_deleted");
}

The output of these few lines example proves it is working as expected:

root [0] .x delete_a_file.C
Processing delete_a_file.C...
ls: file_to_be_deleted: No such file or directory
file_to_be_deleted
ls: file_to_be_deleted: No such file or directory

Hi,

now I see. As I said, no, it works for any file :slight_smile: That’s the nice part.

Cheers,
D

PS
Apologies: I did not see Olivier replied already! :slight_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.