Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Closing files with extensions

Status
Not open for further replies.

OrbitUK

Programmer
Apr 19, 2006
3
GB
Hi all - I have some code which opens multiple files with extentions such as .1 .2 .3 etc.. I want to be able to delete these files at the end of code execution to 'clean up' for the user.

I have a TK interface with underlying TK/TCL code. However I am getting a permission denied on deleting the files until the TK widget is closed (so in fact the program is keeping these files open despite the I/O channel being closed). I am actually closing the I/O channel for each file but am wondering if its still keeping open the .1 .2 .3 extentions. I havent yet figured out a way of closing these kinds of files..

Any ideas?
 
Hi

Could you post ( the relevant part of ) your code ? I tried it like this, and works fine :
Code:
button .bo -text "open" -command {
  for {set i 1} {$i<=3} {incr i} { set F($i) [open "myfile.$i" "w+"] }
}

button .bw -text "write" -command {
  for {set i 1} {$i<=3} {incr i} { puts $F($i) "$i * $i = [expr $i*$i]" }
}

button .bc -text "close" -command {
  for {set i 1} {$i<=3} {incr i} { close $F($i) }
}

button .bd -text "delete" -command {
  for {set i 1} {$i<=3} {incr i} { file delete "myfile.$i" }
}

pack .bo .bw .bc .bd
I got no error message on Linux with Tcl/Tk 8.4, neighter when I deleted the files without closing them.

Feherke.
 
Thanks for your reply, im still getting the file permissions error. Heres the code :-

set CustomerCopy [open $DirPath r]
foreach line [split [read $CustomerCopy] ~] {
set WriteFile [open Cleaned.$fileno a+]
puts $WriteFile "$line"
close $WriteFile.$fileno

This basically reads a bunch of files in a directory and from that creates the files Cleaned.1 Cleaned.2 Cleaned.3 ... etc..

On each iteration of the code the variable $fileno is incremented. If I directly delete one of the files using

file delete "Cleaned.1"

It gives a file permission error.
 
You're not really closing the file you think you are.
set WriteFile [open Cleaned.$fileno a+]
This opens Cleaned.1, for instance, and assigns the return value to WriteFile.

Then
close $WriteFile.$fileno
closes a [red]different[/red] file, <whatever returned from the open>.1

Obviously, what I'm trying to say is to just use close $WriteFile

_________________
Bob Rashkin
 
Hi

That looks strange. The [tt]close[/tt] command expects a channelId as parameter. Your channelId is kept in the variable $WriteFile, so you have to pass only that as parameter to the [tt]close[/tt] :
Code:
[gray]...[/gray]
close $WriteFile

Feherke.
 
I have tried

close $WriteFile

still get the permission denied on deleting Closed.1. Heres more of the code if its any use - I create a directory called Cleaned and move the resulting files there (not the Cleaned.x files)

cd [file dirname $DirPath]
file mkdir Cleaned
set CustomerCopy [open $DirPath r]
foreach line [split [read $CustomerCopy] ~] {
set WriteFile [open Cleaned.$fileno a+]
puts $WriteFile "$line"
close $WriteFile
}
if {[string length $FileE] == 0} {
set FE [split $DirPath .]
set FileExt [lindex $FE 0]
} else {set FileExt $FileE}

RemoveCert $fileno $FileExt
file copy -force $FileExt.$fileno Cleaned
incr x
}

close $CustomerCopy
file delete "Cleaned.1"

I call a proc called RemoveCert to basically perform text removal in the file. Afterwards it just returns to this procedure and copies over the 'cleaned' up files to the Cleaned dir.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top