I am looking for help.
For the purpose of the question, i have created a small test program. I am trying to find a way to import an input that is saved in a variable from one Python file, into another file. I am able to pass a standard variable, but not one that has been inputted by a user.
When i try to do this it runs the file from the start again, which is not what i want See below
I want to Run test.py first
test.py
password = input ("Please enter your password")
import test2
test2.py
from test import password
print (password)
output below
Please enter your password:Fred
Please enter your password Fred
Fred
What i am trying to do is to pass an input without it running the file again. I hope this makes sense.
I basically want the second file to display the input
Related
I'm currently building an user friendly program in Python. Currently, the user is able to modify the input values provided in an init script that we can call init.py. At this moment the user can open in Spyder, the main.py and run/execute the whole process or just typing the classical command:
python3 main.py
The main.py file import all the variables needed from the init.py and run normally. What I would like to do now is to add a feature which allows the user to change the name of the init.py file. For example to be able to build initcustom.py.
And use the following command :
python3 main.py initcustom.py
How can I be able to import variables in main.py from a script which can change name (that should be provided by the user in the command line)?
And in the case where nothing is specified we keep the classical init.py
What such feature will induce as changes in the case where someone just want to do F5 using Spyder without precising input names?
Thank you in advance for your help
You can probably do something with exec
import sys
exec(‘import’+’sys.argv[2]’)
What is the best method to hide credential in a Python script?
I would like to avoid storing it in clear text.
import paramiko
# how to avoid clear text ?
my_server='myserver'
my_password='mysecret'
ssh = paramiko.client.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(my_server, username='root', password=my_password)
except paramiko.SSHException:
print("Connection Failed")
quit()
If I create an myscript.exe with pyInstaller, is it possible to retrieve the clear text (my_server and my_password) "disassembling" the .exe?
Thanks
It doesn't matter if it's stored in clear text or not, if the script needs the password, everyone who has the script also has the password.
Store the credentials in a separate file (yaml comes to mind) and load that at runtime. Don't add the credentials file to the repository if every user uses their own credentials.
I use a program which, sadly corrupts some saved files at random times. To be helpful (although I am a novice at this) I am trying to make a Python program to basically backup those file from the AppData/local directory and put them in a folder on C. I need this program to overwrite the previously copied files each time it is run.
I need to generalize the AppData/local because each person who uses this program would, in theory, have a different user directory preceding the AppData folder.
I've tried running some of my own attempts at a solution.
I will post the results.
# Imports
import shutil
import os
import distutils
from distutils import dir_util
# Paths
# os.makedirs("C:/RevSaves-Backup")
path = '%LOCALAPPDATA%/Remnant'
backup_path = "C:/RevSaves-Backup"
# Procedures
print("The Very Basic Remnant Save Backup Utility")
print(" ")
print("Backing up the save source:")
print(path)
print(" ")
print("It is recommended you run this at regular intervals \nto ensure you have the latest saves up to date.")
distutils.dir_util.copy_tree(path, backup_path)
print("Backup completed.")
When I execute this via command prompt or PowerShell, I get the following message:
Traceback (most recent call last):
File "RevSaveBkUp.py", line 28, in
distutils.dir_util.copy_tree(path, backup_path)
File "C:\Users\candr\AppData\Local\Programs\Python\Python37-32\lib\distutils\dir_util.py", line 124, in copy_tree
"cannot copy tree '%s': not a directory" % src)
distutils.errors.DistutilsFileError: cannot copy tree '%LOCALAPPDATA%/Remnant': not a directory
I am having trouble "targeting" the system-specific local AppData folder.
After a lot of reading, I made the following solution if anyone else is trying to do something similar. I do not know if this is the "best" or "right" way of doing things, however.
Here is how I targeted the AppData Local folder regardless of the user logged in:
path = os.path.join(os.path.expanduser('~'), 'AppData', 'Local')
Some explanations for anyone who is new like me:
os.path.join basically connects folders together in the path. For example, using the above code, join would "connect" AppData to Local and the "User Folder" (referenced in the code as '~'). The output would look like this: C:\Users\your_username\AppData\Local
os.path.expanduser defines the user in question. For example, "~" targets the current user logged in. It goes inside the () because this is how you tell "your code" who, to target. If you wanted a specific user (if you had more than one) you could possibly use os.path.expanduser('Jane') I believe.
Keeping the notes above in reference, this method allowed me to define the variables I needed to and use them for the copy above, where I could not normally use the AppData directory as I wanted.
This was done by using the following code as an example:
path = os.path.join(os.path.expanduser('~'), 'AppData', 'Local')
backup_path = "C:/MyBackupFolder"
Finally we executed the copy with this:
distutils.dir_util.copy_tree(path, backup_path)
The above copied The AppData information I needed to the backup folder.
I hope this helps everyone learn as I did, it came in quite handy.
I have a python code and I am saving the results in a destination with specific file name, this file name will change every time and it is a recurring event.
Here is my code:
import csv
outfile=open('path.macrovariable.csv','w',newline='')
writer=csv.writer(outfile)
writer.writerow(["Jobname","employement","company","descrption","location"])
writer.writerows([job_name])
writer.writerows([emplomnt_type])
writer.writerows([organisation])
writer.writerows([job_descrption])
writer.writerows([job_location])
In the outfile the file name here as "macrovariable" will change every time. I want to create a macrovariable at the top of the program which will be called later in the program in the place of "macrovariable" instead of hardcoding.
Thanks & regards,
Sanjay.
I'm coding Python and I have this problem that I searched for throughout this website and others and I still can't find an actual answer for this problem I am facing. The best way to ask this is by showing the code first:
def showFile():
with open("Password.txt","r") as file:
userShowType = input("What password do you want to show? \n")
for line in file:
if userShowType in line:
print (line)
What I want the program to do is for the user to enter the type of password (e.g. Outlook, Gmail, Youtube, etc) and the computer to search for it. If the computer finds the word, it will print the entire line that contains both the type of password and password. If it doesn't, then the computer will print a line telling the user that the type of password doesn't exist or that the comptuer couldn't find it.
However, the above code does not perform the latter of the if statement, only the former and prints out if the computer found the password. Is there any way in which I can do both?
Since the default answer is a message stating the password wasn't found, by pre-seeding such message & only updating it if the test criteria is met will produce the desired outcome
def showFile():
with open("Password.txt","r") as file:
userShowType = input("What password do you want to show? \n")
msg = 'passwd not present in file'
for line in file:
if userShowType in line:
msg = line
break
print(msg)
The for loop over lines will only update msg if the requested password is found (and equally break out of the loop). The the final print will display the message