Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 03-28-2019, 07:29 PM
slouw slouw is offline
Registered User
 
Join Date: Nov 2005
Posts: 158
Writing to File

This test script was working now gives me this message below.
I cribbed it from a sample script SaveSelectionToFile.py off vandyke.com.
I think I got it here https://forums.vandyke.com/showthread.php?t=10365 but I see there is no python version only VBA version now. Significant?
I noticed at the time the reference script did not have an explicit open verb in action.
Is it better to open a file?

Quote:
IOError
Error:
File: C:\Users\XXX\SecureCRT\Scripting\file.py

Line: 5
fp = file("C:\Users\XXX\Data\Python\file_py.log", fileMode)

---------------------------
OK
---------------------------
Testing script:
Quote:
# $language = "python"
# $interface = "1.0"

fileMode = "w"
fp = file("C:\Users\xxxx\Data\Python\file_py.log", fileMode)
fp.write("Line 1111111111111\n")
fp.write("Line 22222 22222\n")
fp.close()
Reply With Quote
  #2  
Old 03-28-2019, 08:57 PM
slouw slouw is offline
Registered User
 
Join Date: Nov 2005
Posts: 158
Missing backslash between path and filename

The problem is a missing backslash.
For some reason you need 2 backslashes between file path and filename.
Why?
In the script above this is WRONG:
Quote:
fp = file("C:\Users\xxxx\Data\Python\file_py.log", fileMode)
This is corrected:
Quote:
fp = file("C:\Users\xxxx\Data\Python\\file_py.log", fileMode)
Why is this needed?
I remember wondering about it at the time now.
Is this just something I must remember?
Reply With Quote
  #3  
Old 03-29-2019, 08:27 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,544
Hi slouw,
Quote:
I noticed at the time the reference script did not have an explicit open verb in action.
Is it better to open a file?
Correct, VBSscript has no such function. Yes, in Python, I've always seen open() used in our script examples for file handling.


Quote:
The problem is a missing backslash.
For some reason you need 2 backslashes between file path and filename.
Why?
In the script above this is WRONG:
It's not wrong in VBScript. This is a Python requirement, not a SecureCRT one.

https://docs.python.org/2.0/ref/strings.html
https://stackoverflow.com/questions/...-not-to-escape

VBScript is only supported on Windows, where \ (backslash) is the path separator. Since Python can be run on Mac/Linux, the default path separator is / (forward slash) so the backslash needs escaped. There are alternatives suggested in the StackOverflow post above.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #4  
Old 03-29-2019, 05:51 PM
slouw slouw is offline
Registered User
 
Join Date: Nov 2005
Posts: 158
Hi Brenda. thank you for your reply. It is greatly appreciated.
I now understand the backslash item.
So to the outstanding quuestion.
Is it good practice/needed/etc to use an open() statement in python?
Below is the working script complete with extra backslash.
No open statement though as you can see.
Please note python script, not VB.
It seems an open() function does exist in python
https://www.w3schools.com/python/python_file_open.asp
But not necessarily mandatory?


Quote:
# $language = "python"
# $interface = "1.0"
fileMode = "w"
fp = file("C:\Users\shaunl\Dropbox\Data\Python\\file_py.log", fileMode)
fp.write("Line 1111111111111\n")
fp.write("Line 22222 22222\n")
fp.close()
Once again appreciate any replies
Reply With Quote
  #5  
Old 04-01-2019, 08:32 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,544
Hi slouw,
Quote:
Is it good practice/needed/etc to use an open() statement in python?
That would seem to be a matter of personal preference if both functions accomplish the same thing.

If file() accomplishes what you want and you are happy with it, by all means use file().

From Python documentation of file(). [Bold emphasis added by me.]:
Quote:
file(name[, mode[, buffering]])
Constructor function for the file type, described further in section File Objects. The constructorís arguments are the same as those of the open() built-in function described below.

When opening a file, itís preferable to use open() instead of invoking this constructor directly. file is more suited to type testing (for example, writing isinstance(f, file)).
https://stackoverflow.com/questions/...e-file-vs-open

From the above post, it seems file() was phased out in Python v3.0 and while SecureCRT/Windows is using Python v2.7, we do hope to go to v3.0+ eventually.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #6  
Old 04-08-2019, 03:46 AM
ogun ogun is offline
Registered User
 
Join Date: Sep 2013
Posts: 15
To explain the double backslash, backslash is used as an escape sequence to insert control characters into text string.

So for example
HTML Code:
"banana\n"
will be banana followed by a newline.
Double backslash is the way to insert a literal backslash in the string.

The other options is to use a raw string, prefixed by r.

So in your case, the following two options are equally valid:
Code:
fp = file("C:\\Users\\shaunl\\Dropbox\\Data\\Python\\file_py.log", fileMode)

fp = file(r"C:\Users\shaunl\Dropbox\Data\Python\file_py.log", fileMode)
Note the 'r' in front of the second string, this disables the escape sequence handling.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 07:16 AM.