VanDyke Software Forums

VanDyke Software Forums (https://forums.vandyke.com/index.php)
-   Scripting (https://forums.vandyke.com/forumdisplay.php?f=14)
-   -   Phyton scripts after upgrade (https://forums.vandyke.com/showthread.php?t=14481)

Chris_T 03-31-2021 11:32 AM

Phyton scripts after upgrade
 
Hi Gents,

We have upgraded SecureCRT from Version 8 to Version 9.0.1

For long we have been using Python scripts and never had issues. With the new version all the scripts seem to not work anymore and and seem to fail to address files and folders (see below, simply opening file object within the local script folder not working anymore):

----------------------------------------------------------
IOError

Error:
File: C:\Users\...\script.py
Line:14

f = open(file1,'w')
---------------------------------------------------------


Not a python geek though, but is there a workaround to address this? Updating all scripts will be a pain..

Many thanks, Chris

cboyack 03-31-2021 02:47 PM

Hi Chris,

There are a lot of variables that come into play with regards to scripting issues. In this particular case, we're going to need some more information about your setup in order to troubleshoot further. Could you please answer the following questions?
  • Which version of Windows are you on?
  • What is the exact version of SecureCRT from which you upgraded to version 9.0.1?
  • Is this upgrade something that happened in-place from the older version of SecureCRT to the newer version on the same machine, or did you move to another machine in the process of upgrading SecureCRT?
  • What is the encoding of the file your script is attempting to load? (ANSI, UTF-8, UTF-8 with BOM, etc.)
  • What exactly does the "file1" variable value reflect? A file name alone? A full path to a file?
--Cameron

Chris_T 04-06-2021 04:18 AM

Hi Cameron, thanks for checking.

I have upgraded from SecureCRT version 8.3 on my Windows 10 PC, which is still the same as before.
With the old version we were able to open files from the local folder we started the .py file from, see below:

Code:

    outputfile = str(date.today())
    file1 = ("Inventory-" + outputfile + ".csv")
    f=open(file1,'w')

This now is not possible anymore. I have to exactly specify the path where to open the file:

Code:

localfolder = r'C:\\Users\..\\..\\..\\'
   
    outputfile = str(date.today())
    file1 = (localfolder + "Inventory-" + outputfile + ".csv")
    f=open(file1,'w')

This is not very convenient, as the folder might change from time to time. And for now I am not sure how to let python find the local folder by itself., this i have to investigate.

Regards, Chris

cboyack 04-06-2021 01:30 PM

Hi again Chris,

In all honesty, it seems like a miracle that the scripts worked as nicely as they did for you with the older version of SecureCRT, as the current working directory can mean a lot of different things, and it just happened to land nicely for you in the past. Take the following script, for example:
Code:

# $language = "Python"
# $interface = "1.0"

import os
strDir1 = os.getcwd()
strDir2 = os.path.dirname(os.path.realpath("."))
strDir3 = os.path.dirname(os.path.realpath(__file__))
strDir4 = os.path.dirname(crt.ScriptFullName)
crt.Dialog.MessageBox("dir1: {}\ndir2: {}\ndir3: {}\ndir4: {}".format(
    strDir1,
    strDir2,
    strDir3,
    strDir4))

If you run this script, you can see how, depending on the context, different versions of the current working directory can result in a file being written/opened in wildly different locations. If you're simply looking to have the file be written within the same directory in which the .py file was run, please consider using the format from line 7 or 8 from the above script to obtain that directory for further use in your script.

If you're looking for some other behavior in which the script will open/write to a file in some other location, what's the anchor for that location?

Chris_T 04-07-2021 01:47 AM

Hi Cameron,

our scripts were doing the job, so there never was a reason to think about or to change the way files were addressed. Anyway, with your input on this it seems to be the best to do an overhaul on them.

Many thanks for your efforts!!

cboyack 04-07-2021 08:13 AM

Hi Chris,

Happy to help.

I'm sorry it's not a simple point and click fix, but I agree with you that making the change to point to the proper relative directory is a good thing to do now - to protect you against similar issues cropping up in the future with any other potential SecureCRT version upgrades :)


All times are GMT -6. The time now is 11:33 AM.