Using: Windows 7, Python 2.7
Code:
Dr Edit - Google's example for creating a new file in Google Drive
Dr Edit Using Python with Google Drive SDK
The README instructions state:
Create a session secret, which should be at least 64 bytes of random
characters, for example with
python -c "import os; print os.urandom(64)" > session.secret
I have no idea how to run this command line. I've tried to run it in the Windows Command Prompt. I've tried to run it in Python Shell. I've tried to run it in the Python Commmand Line. SyntaxError: invalid syntax
Is this README doc correct? Is it outdated? Am I doing something wrong? Is this a line of code that is supposed to be part of a python program? What is the -c for?
I just realized the the python -c is probably just notation to indicate that the user should use the python command line to enter what follows.
So I entered:
import os
print os.urandom(64)
And a string of special characters were printed:
タAËDkæ4ÃZヌTツUルヒL゚é-ツؓルト0ᄚ'Ënᄚô#ߝUíèRKRüÏ*'ᄇᄂヤナ%ÿëᄄÄÓò&
There is probably supposed to be a session.secret object? file? variable? that the string of special characters get written to.
I needed to learn the Python Print function.
You can read and write files in Python without importing a library.
The Open() function in Python will open a file, OR create a new file
There are four modes to open (create) a file
'r' read only
'w' write only
'a' append
'r+' both reading and writing
This will create a new text file named "daSecretCode" if there wasn't one already. NOTE: The file gets created in the directory from where the .py module was run from.
daNewFile = open("daSecretCode.txt", "w")
Write content to file:
daNewFile.write("oerjfwi456745oetr78u9f9oirhiwurhkwejr")
Close the file:
daNewfile.close()
Write a new session secret to a new file:
import os
daSecretCode = repr(os.urandom(64))
daNewfile = open("daSecretCode.txt", "w")
daNewfile.write(daSecretCode)
daNewfile.close()
I'm not sure how I could format the content to look like the original output. The characters get formatted into something else.
Related
I'm following along with Google's Python class, and the person in the videos always runs his scripts from the interactive session in command line using "./". Whenever I try it, I just get a syntax error. How can I use ./ to run scripts? I'm using Windows 10
To run a script from the command line you need to use the syntax
python3 script.py
Now on Unix systems, it's possible to add a shebang to the first line of the script as followings
#!/usr/bin/env python3
This then allows the shell syntax './name.py' to work. But windows doesn't have this mechanism. Instead, you need to create an 'association' between the .py extension and the python executable ('right click', 'open with'). Or just use the full syntax. Both require the python executable to be in your path, and generally on windows both python 2 and 3 will have the same executable name
I have been trying enter couple of commands on a command window with python script. I was able to open the .exe file by checking the other questions.
However, now my code:
import os
os.chdir(r'filepath')
os.system("start /B start filepath\application.exe")
opens the application and it asks for an input file name, then it will ask for output file name. Is there a way to do that. This will be used for ArcGIS so it needs to be done in python 2.7.
I checked all related questions but did not find the answer. I would appreciate it very much if you could help.
Finally! It was easier than I thought but I needed lots of research. This code for python 2.7 which may differ for other versions.
import os
import subprocess
os.chdir(r'filepath')
subprocess.check_call([r"filepath\your_.exe_app", 'your_input'])
This is the first time I tried doing a shell script.
So this is what I got for now:
$pdf = read-host "enter the pdf name"
cmd /k C:\the path\\./PDF_ID.py $pdf /all> C:\the path\data.txt
C:\the path\pdf.py
So the first python script is executed and it saves the output to the data.txt but I dont know how to run the second python script that analyzes data.txt and outputs if the pdf file contains malware or not.
If you are able to run Python from Powershell, then python <scriptName>.py will get your job done.
You can give the full path of the Python executable within the command line (incase not working). You could check and ensure that your python executable path is available in your system variables.
If you are not looking from any concurrent solution, then directly put these two inside your samplePS1 file:
python firstscript.py ;
python secondscript.py ;
Note: If you want to pass arguments , then you can pass the arguments also. This is a sequential execution.
Hope it helps you.
I am new to python, just got the learning python book and got stuck with the spam.py in the command line. The book says to make a file named spam.py and then ask python to run this by typing
%python spam.py
I have added the python to my PATH as it was C:\Python27 so I can call Python in the Windows CMD, but this just will not run. The error I receive is
>>>python spam.py
File "<stdin>", line 1
python spam.py
^
SyntaxError: invalid syntax
I appreciate any help that you can give.
Your problem is that you're trying to run your code from within the Python interpreter itself (the >>> prompt is the giveaway here since that's the Python prompt).
Exit from the interpreter (with CTRL-Z and ENTER for Windows) and run it from cmd.exe (the c:\> is the prompt in the example below):
c:\> python spam.py
From within the interpreter, you can also run an external file with:
execfile('spam.py')
Could you post the code from spam.py...
You seem to be trying to run the spam.py from the Python interpreter. Go to where the file is in Windows Explorer and launch it from there, using the C:\Python2.7\python.exe CLI.
By the way, since you didn't understand the syntax error warning, please see:
http://dictionary.reference.com/browse/SYNTAX
http://dictionary.reference.com/browse/SEMANTICS
You are trying to execute Python script file within the interpreter. Come out from the Python interpreter by pressing CTRL+Z and then ENTER key.
Then execute with the command :
Say, C:/> python spam.py
In order to run a python program you have to run program in Command Line not in Python Interpreter (press Windows Sign + R and type cmd.exe)
Moreover you have to remember to be exactly in the directory where your file is saved, e.g.:
If the file file is C:\Python27\spam.py you have to be in C:\Python27.
To change the directory:
use dir to display the folders and files in current place
use cd to change your directory (e.g. C:\Python27\>cd Spam moves you to C:\Python27\Spam
use Tab key to autocomplete names of the commands, folders and files
As you said you have added Python to PATH and followed my instructions, the statement below should work perfectly
python spam.py
Hope I could help.
If for example, your file is in G: drive, type this in cmd:
python G:/myfile.py
Basically, type in the path. Just doing "cd" won't work in Python
I am trying to run my celcius.py file but I don't know what to type in the command line...
When I type Python celcius.py it does not work:
I get the following message:
>>> python celcius.py
File "<stdin>", line 1
python celcius.py
^
SyntaxError: invalid syntax
>>>
When I open it the program with the Python IDLE GUI it runs the program and closes the program before I can see it. I read somewhere that it won't close if you run it from the command line.
You need to type "python celsius.py" in the windows command prompt, rather than the Python command prompt; this only works straight away if the Python directory is in your PATH. It probably isn't, so you need to use this instead: C:\Python27\python.exe celsius.py.
Because celsius for you is in the same folder as python, you can do this as well:
CD C:\Python27
python.exe celsius.py
In general, change the directory (CD) to where your file is stored, then run C:\Python27\python.exe <filename>
Double-clicking is generally easier in Windows, though, assuming it is set up to do that (it normally is unless you change it back)
There are a few ways of doing this.
WAY 1
step one: Open command prompt (go to start and type in "cmd") *NOTE: do NOT open python. if you see ">>>" in your command prompt window you opened python. This is bad.
step two: type in this: python "path\to\file\celcius.py" replacing path\to\file with the path (starting with the name of the folder that you're computer user is named. So for my computer that would be ben. So let's say my celcius.py is located on the desktop I would run python "Desktop\celcius.py" and hit <.enter.>. It looks like you have celcius.py in your python folder. Don't put it there. that's the system files for python itself. move it to your desktop or your documents folder. By the way, if you have truble using this, make sure you're system is set up to execute the python command. Go here for reference.
WAY 2
assuming you have python associated with the .py file in explorer, you can find the file and double-click on it.
WAY 3
open IDLE (you can usually find it in your list of programs in start). Open the python file through IDLE. This will show you the source code for your program. hit <.F5.>. F5 will execute the program from within the the IDLE shell.
You don't need to do this, just open the command prompt and type:
python path/to/your/file.py
Just make sure that python command is set in your environment PATH, check this