I am studying automate the boring stuff with python book and therein after finishing the password locker project it is suggested to create a batch file using the following codes:
#py.exe C:\Python34\pw.py %*
#pause
and I did exactly the same mine one looks like this (copied entire script if there are any other potential errors):
import sys
import pyperclip
""" PASSWORD LOCKER """
passwords = {
'facebook' : 'password of facebook',
'gmail' : 'password of gmail',
'quora' : 'password of quora'
}
if len(sys.argv) == 2:
account_name = sys.argv[1]
if account_name in passwords:
print('Password of [' + str(account_name).upper() + '] has been copied to clipboard.')
acc_password = passwords[account_name]
pyperclip.copy(acc_password)
print('Paste it anywhere')
else:
print('There is no name registered with that account name.')
#py.exe 'C:\py\Automate the Boring Stuff with Python\Data Structures, Dictionary\pw.py' %*
#pause
then saved the file as pw.bat, according to instruction of the book:
With this batch file created, running the password-safe program on
Windows is just a matter of pressing win-R and typing pw .
I then followed these steps again and it didn't work fine. Can you please help me with this. Thank you.
Here are troubleshooting steps I'd suggest:
Ensure py.exe exists in environment path of your computer. The book may have installation instructions that may have been skipped inadvertently. If you know py.exe exists on your computer, but when you execute py.exe on opening a command prompt, you may see:
'py.exe' is not recognized as an internal or external command, operable program or batch file.
If you see the above message, this implies, you need to do either of the following:
a. Change your batch file to say:
`#<drive:\path\to>\py.exe`
where the part <drive:\path\to> is the path to your python executable (py.exe), OR,
b. Add the path to py.exe to the environment variables. Comprehensive instructions are here for Python 3.
The way you have framed the question seems to suggest that you saved the entire code in one file pw.bat. If you did, you need to ensure the following:
a. Last two lines of the your code snippet are in a file pw.bat, AND,
b. Rest of the code is in the file C:\py\Automate the Boring Stuff with Python\Data Structures, Dictionary\pw.py.
The instruction below is correct only if the file pw.bat is in the environment path and thus visible to the operating system:
With this batch file created, running the password-safe program on Windows is just a matter of pressing win-R and typing pw
If you do not know whether this is true or not, you will need to find out where you saved pw.bat. Open a command prompt and navigate to that location, and then execute pw. Issues pertinent to this are discussed here.
Related
When I try to write something, such as variables, the code is renamed to the file name on the computer.
For example, if I write:
a = 20
f = 15
print(a+f)
then the code file will automatically be renamed to the first line, i.e. "a = 20"
Then, when I try to run the code, the program outputs nothing but "Python" and some incomprehensible words.
What could it be related to?
enter image description here
enter image description here
I installed the latest version of Visual Stuio Code with Python, they are new, so there should be no problems. But this time it went wrong.
After reinstalling the program, the problem remains.
First of all, if there is no special requirement, please do not use Code Runner to run the script, using the official extension Python is a better choice.
In addition, the dot on your file label means that you have not saved the file, you can add the following setting to enable automatic saving in the settings.
"files.autoSave": "afterDelay",
You may have created the file using the following method. File --> New File... --> Python File. At this time, the file has not been named, also not saved. You can see that there is no such file in the resource manager list at this time.
So the file label shows the first line of codes. This is a feature of vscode, you can refer to this link. And because the file has not been saved, there will be problems executing the script.
You can rename the script file directly (F2), or vscode will remind you to name the file when saving. Another way to create a file is to right click and choose New File..., enter filename and end with .py extension.
I am trying to replicate another researcher's findings by using the Python file that he added as a supplement to his paper. It is the first time I am diving into Python, so the error might be extremely simple to fix, yet after two days I haven't still. For context, in the Readme file there's the following instruction:
"To run the script, make sure Python2 is installed. Put all files into one folder designated as “cf_dir”.
In the script I get an error at the following lines:
if __name__ == '__main__':
cf_dir, cf_file, cf_phys_file = sys.argv[1:4]
os.chdir(cf_dir)
cf = pd.read_csv(cf_file)
cf_phys = pd.read_csv(cf_phys_file)
ValueError: need more than 0 values to unpack
The "cf_file" and "cf_phys_file" are two major components of all files that are in the one folder named "cf_dir". The "cf_phys_file" relates only to two survey question's (Q22 and Q23), and the "cf_file" includes all other questions 1-21. Now it seems that the code is meant to retrieve those two files from the directory? Only for the "cf_phys_file" the columns 1:4 are needed. The current working directory is already set at the right location.
The path where I located "cf_dir" is as follows:
C:\Users\Marc-Marijn Ossel\Documents\RSM\Thesis\Data\Suitable for ML\Data en Artikelen\Per task Suitability for Machine Learning score readme\cf_dir
Alternative option in readme file,
In the readme file there's this option, but also here I cannot understand how to direct the path to the right location:
"Run the following command in an open terminal (substituting for file names
below): python cfProcessor_AEAPnP.py cf_dir cf_file cf_phys_file task_file jobTaskRatingFile
jobDataFile OESfile
This should generate the data and plots as necessary."
When I run that in "Command Prompt", I get the following error, and I am not sure how to set the working directory correctly.
- python: can't open file 'cfProcessor_AEAPnP.py': [Errno 2] No such file or directory
Thanks for the reading, and I hope there's someone who could help me!
Best regards & stay safe out there during Corona!!
Marc
cf_dir, cf_file, cf_phys_file = sys.argv[1:4]
means, the python file expects few arguments when called.
In order to run
python cfProcessor_AEAPnP.py cf_dir cf_file cf_phys_file task_file jobTaskRatingFile jobDataFile OESfile
the command prompt should be in that folder.
So, open command prompt and type
cd path_to_the_folder_where_ur_python_file_is_located
Now, you would have reached the path of the python file.
Also, make sure you give full path in double quotes for the arguments.
I am attempting to run this .PY file from Command Prompt:
# Merge two .BSG files
# Starting block and world position are taken from the first file
# Example: "bsgmerge input.bsg output.bsg merged.bsg"
import io, sys
one = open(sys.argv[1]).readlines()
two = open(sys.argv[2]).readlines()
for n in [1,3,5,7,9,11,17,19,21,23]:
one[n] = one[n][:-1]+"|"+two[n].partition("|")[2]
open(sys.argv[3],"w").write("".join(one))
It is a program that takes a creation from the game Beseige and merges it with another saved creation so that opening the merged file results in both creations being present. If you want more details, you can read up on that here.
I am having trouble figuring out how to call this program from the command line. At first I thought the problem was me having Python 2 (it requires Python 3), so I uninstalled 2 and installed 3. This did not help.
What I am doing is entering the "python" command to pull up the Python environment within CMD, then entering the command to call the program based on the third comment in the file ("bsgmerge input.bsg output.bsg merged.bsg").
I tried using full file paths or simply changing to the correct directory before typing the "python" command and using only the file names, but so far I've had no luck.
When I am in the correct directory, then enter the Python environment, typing the command "bsgmerge 1.bsg 2.bsg M.bsg" (my existing files to be merged are 1.bsg and 2.bsg), this error occurs:
File "<stdin>", line 1
bsgmerge 1.bsg 2.bsg M.bsg
^
SyntaxError: invalid syntax
I took a Python course (which is why I used to have Python 2 on my machine) last fall, so I noticed that there is no "def" defining a function in the above code, which is something I've never encountered, so I'm thinking that is the root of my problems.
Thanks in advance for the help.
I was probably same problem with python launcher.
If you use Linux, first line shoud be:
#! /path/to/your/python/3
In Windows it some more complicated:
In registry by regedit change
HKEY_CLASSES_ROOT\Python.File\shell\open\command from "C:\Python27\python.exe" "%1" %* to "C:\Windows\py.exe" "%1" %*.
And first line of script shoud be:
#! python3
Now it shoud work properly.
So i've searched through all the questions on this site (maybe not all, but most), and none of them have quite the right answer for what i'm looking for!
Part of my code, in Python, is setup as:
specialstring = special
if input == "T"
trash = commandline("sudo dd if=zero blahblah%blah" % specialstring)
Every time I run the command, it asks for a password. I do not want it to request a password! I don't need a lecture on how unsafe it is to run a root without a password (or however your phrase it)... I would just like to know what to do to have my code not need a password to run the command, and then exit root after the command so I can continue on normally with the rest of my code. THANKS!
-NOOB
You can make a "user" not have to enter in the password when using sudo for a specific program by editing the /etc/sudoers file.
Open up the file and edit it to include this line (where is the name of the user):
<user> ALL = NOPASSWD: /bin/dd
As mentioned in the comments of your previous question, you need to add to your sudoers file like this for the sake of simplicity just turn off the passwords and you will be able to run this without a password
admin ALL = NOPASSWD: ALL
the file will be found in /etc/sudoers
Then your python script will run fine and not require a password
Here is a tutorial on how to use visudo which is what you will need to use to edit sudo password settings
Why would you need to run 'dd' as root anyway? The 'dd' program is available to all users, so running the same command without 'sudo' in front would be sufficient. The only reason I can think of is if the file where you write the output of 'dd' to is only writable by root or another user.
So, try this:
specialstring = special
if input == "T"
trash = commandline("dd if=zero blahblah%blah" % specialstring)
Is there a simple module that let's you paste input in python ?
Asking someone to type letter by letter is kinda harsh .
By default a .py file is opened with python.exe if is installed and this does not allow "rightclick+paste" in the console .So how can i make this happen with python ? i think this would be more of a exact question .
You can make this by open cmd.exe and type here "C:\Python32\python".
Path is depend on the version of python. Mine is 3.2.
If you're looking for a way to simply paste something into the windows command prompt, John Giotta is correct that a user can click on the little icon in the top left.
I imagine, however, that you're looking for a way that a user can input a large amount of text, without typing it in line by line. A simple way to do this, would be to let the user input a file name, which python would then read. Perhaps something like this is what you're looking for:
while True:
filename = raw_input("Path to file to be read: ")
try:
with open(filename, 'rb') as f:
contents = f.read()
break
except IOError:
print "That was not a valid file \n"
This loop will keep asking the user for a file until then enter a valid path. When they enter a valid path, it will be read in as a string to the contents variable. This way, a user could enter a large amount of text into a file, and then you simply prompt them for the path to the file.
You can read up on file input more In the docs.