Problem with paths to files 
Author Message
 Problem with paths to files

Any help appreciated.
In Windows 98 I have two scripts and one file:

Script  "one.tcl" :
#catch [set f [source C:\\Test\\two.tcl]]
#exit
That is located into "C:\Windows\Desktop\two.tcl"

Script "two.tcl" :
#set dir [file join [pwd] log.txt]
#set fileid [open "$dir" a+]
#set b [read $fileid]
#puts $fileid $dir
#close $fileid
#exit
That is located into "C:\\Test\\two.tcl"

And file "log.txt":
<blank file>
That is located into "C:\\Test\\log.txt"

When I double click on "one.tcl" I get "two.tcl" write into"log.txt"
the follow: "C:\Windows\Desktop\log.txt".
What I want is "two.tcl"  write "log.txt" real path , I mean, where
"two.tcl and "log.txt" are located, not where "one.tcl" is located.
What I am doing wrong ?

Thank you



Fri, 30 Jul 2004 21:30:38 GMT  
 Problem with paths to files

Quote:

> Any help appreciated.
> In Windows 98 I have two scripts and one file:

> Script  "one.tcl" :
> #catch [set f [source C:\\Test\\two.tcl]]
> #exit
> That is located into "C:\Windows\Desktop\two.tcl"

> Script "two.tcl" :
> #set dir [file join [pwd] log.txt]
> #set fileid [open "$dir" a+]
> #set b [read $fileid]
> #puts $fileid $dir
> #close $fileid
> #exit
> That is located into "C:\\Test\\two.tcl"

> And file "log.txt":
> <blank file>
> That is located into "C:\\Test\\log.txt"

> When I double click on "one.tcl" I get "two.tcl" write into"log.txt"
> the follow: "C:\Windows\Desktop\log.txt".
> What I want is "two.tcl"  write "log.txt" real path , I mean, where
> "two.tcl and "log.txt" are located, not where "one.tcl" is located.
> What I am doing wrong ?

> Thank you

The problem with using 'source' is that it seems to copy the source file
into the current file (the content of two.tcl in the one.tcl file). What
happens is the same as if you physically copied and pasted two.tcl.
In that context the behaviour of Tcl is what you described. I had
similar problems with using source in a procedure and all global
variables in the sourced script became only local to this procedure.

One of the many possible solutions is: make a procedure of the
functionallity in file two.tcl (within the file), with a path parameter
and call that procedure after you sourced two.tcl in one.tcl.

something like:

Script  "one.tcl" :
#source C:\\Test\\two.tcl
#append_2 "C:\\Test\\"
#exit
That is located into "C:\Windows\Desktop\one.tcl"

Script "two.tcl" :
#proc append_2 {path} {
#  set dir [file join $path log.txt]
#  set fileid [open "$dir" a+]
#  set b [read $fileid]
#  puts $fileid $dir
#  close $fileid
#}
That is located into "C:\\Test\\two.tcl"

And file "log.txt":
<blank file>
That is located into "C:\\Test\\log.txt"
--
_______________________________________________________
PETER GIJSEMANS                 ("`-/")_.-'"``-._
Alcatel Bell Space N.V.          . . `; -._    )-;-,_`)
Berkenrodelei 33                (v_,)'  _  )`-.\  ``-'
B-2660 Hoboken - Belgium       _.- _..-_/ / ((.'
phone : ++32(0)3.829.51.87 __((,.-'___((,/_____________



Fri, 30 Jul 2004 23:31:31 GMT  
 Problem with paths to files

Quote:

> The problem with using 'source' is that it seems to copy the source file
> into the current file (the content of two.tcl in the one.tcl file). What
> happens is the same as if you physically copied and pasted two.tcl.
> In that context the behaviour of Tcl is what you described. I had
> similar problems with using source in a procedure and all global
> variables in the sourced script became only local to this procedure.

I see this confusion occasionally.  We should try to overcome it.

Tcl is not C.  Files (or, technically, "compilation units") are
important in C.  There is "file scope" in C (using the "static" keyword).
None of this exists in Tcl.

In Tcl the fundamental thing is the interpreter.  The interpreter evaluates
scripts, and as a consquence, new commands and variables might get defined
in the interpreter.  The only place files enter the picture is that that
[source] command can read a script from a file and evaluate it.  There's
no new scope involved, it's as if you just typed the contents of that file
at an interactive tclsh prompt.

--
| Don Porter          Mathematical and Computational Sciences Division |

| http://math.nist.gov/~DPorter/                                  NIST |
|______________________________________________________________________|



Sat, 31 Jul 2004 00:04:51 GMT  
 Problem with paths to files
Quote:

> Any help appreciated.
> In Windows 98 I have two scripts and one file:

> Script  "one.tcl" :
> #catch [set f [source C:\\Test\\two.tcl]]
> #exit
> That is located into "C:\Windows\Desktop\two.tcl"

> Script "two.tcl" :
> #set dir [file join [pwd] log.txt]
> #set fileid [open "$dir" a+]
> #set b [read $fileid]
> #puts $fileid $dir
> #close $fileid
> #exit
> That is located into "C:\\Test\\two.tcl"

> And file "log.txt":
> <blank file>
> That is located into "C:\\Test\\log.txt"

> When I double click on "one.tcl" I get "two.tcl" write into"log.txt"
> the follow: "C:\Windows\Desktop\log.txt".
> What I want is "two.tcl"  write "log.txt" real path , I mean, where
> "two.tcl and "log.txt" are located, not where "one.tcl" is located.
> What I am doing wrong ?

> Thank you

The [pwd] command is not what you are looking for. Read the docs for [info]!

Try the following:

Script: one.tcl
source C:\\somedir\\two.tcl

Script: two.tcl
puts "PWD is: [pwd]"
puts "info script returns: [info script]"
set logfile [file join [file dirname [info script]] log.txt]
puts "Logging to: $logfile"

Michael Schlenker



Sat, 31 Jul 2004 00:42:00 GMT  
 Problem with paths to files

Quote:

> it's as if you just typed the contents of that file
> at an interactive tclsh prompt.

Only if you type 'source' at an interactive tclsh prompt.  The source command
effectively (with a couple of very minor exceptions) replaces itself with the
content of the named file.  The exceptions are:
  a) [info script] returns a different value.
  b) [continue] and [break] at the top-level of the file behave differently.

That's it.  There isn't even any memory advantage to using [source].

Donal.
--

-- Always running as a superuser is not a fault, it's an OS preference.



Sat, 31 Jul 2004 19:47:35 GMT  
 Problem with paths to files

Quote:
> Any help appreciated.
> In Windows 98 I have two scripts and one file:

> Script  "one.tcl" :
> #catch [set f [source C:\\Test\\two.tcl]]
> #exit
> That is located into "C:\Windows\Desktop\two.tcl"

> Script "two.tcl" :
> #set dir [file join [pwd] log.txt]
> #set fileid [open "$dir" a+]
> #set b [read $fileid]
> #puts $fileid $dir
> #close $fileid
> #exit
> That is located into "C:\\Test\\two.tcl"

> And file "log.txt":
> <blank file>
> That is located into "C:\\Test\\log.txt"

> When I double click on "one.tcl" I get "two.tcl" write into"log.txt"
> the follow: "C:\Windows\Desktop\log.txt".
> What I want is "two.tcl"  write "log.txt" real path , I mean, where
> "two.tcl and "log.txt" are located, not where "one.tcl" is located.
> What I am doing wrong ?

> Thank you

Thank you all replies.

Information was sufficient and let me fix the problem.



Sun, 01 Aug 2004 08:01:01 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Making file path control only select directory paths not file names

2. parser API style - methods take path or file vs sep methods for path and file

3. Idea: virtual elements in path of file(path...)

4. DOS File Lookup - just the file name, not the path AND file name

5. file path sepatator problem

6. Path Problem for INI file

7. Problems with ‘file path’

8. file normalize problem with volumerelative paths

9. Help!!! Encoding file path problem

10. Problem opening files while using os.path.walk

11. REPOST: Re: import path, script path (was Re: module path)

12. Alias TPS with different path/file name than original file

 

 
Powered by phpBB® Forum Software