I can run my script from idle but not from python.exe? - python-2.7

When I run my script from Idle my program runs perfectly, but when I run the .py file and it spawns a shell # C:\Python27\Python.exe my program fails with the following error:-
IOError: [Errno 13] Permission denied: 'my new file.html'
And the bit of code is:-
f = open("my new file.html", "w")
I have searched for this IOError but the things people are saying don't appear to tie in with what I am doing, which is writing out a file?

If it says "permission denied", that's telling you that you don't have permission to create that file. It isn't lying to you. The first rule of debugging is to always assume the error is telling you the literal truth.
Since you didn't supply a folder in the filename, it's trying to create a file in the current directory. You are probably in a protected folder. If you cd to a folder where you have write permissions, the problem will likely go away.

Related

Execute python files from a list

I have made a search of python scripts in some subfolders using os.walk() and endswith(file.py), and put them in a list. Now I'm trying to execute them. So I have a list of the type:
pylist = ['./1/file1.py', './2/file2.py', ...]
and I have tried to execute them using a for loop:
for i in range(len(pylist)):
%run pylist[i]
and also (I'm using a jupyter notebook)
for i in range(len(pylist)):
!python pylist[i]
but in both cases it doesn't find the file pylist[i].py, so it's not taking the content of the list. I also tried
for i in range(len(pylist)):
execfile(pylist[i])
but this gives me an
indexError: list index out of range
with one of the python files, which I don't get if I go directly to the folder and execute the file. One more test was using subprocess, for which I got a permission error:
OSError: [Errno 13] Permission denied
that I suspect it might be because the .py files doesn't have executable permissions. But then, make each file a executable is another problem.
What is the correct way of execute python files in a list?
Edit: More specific
When going to a specific directory on my jupyter notebook and executing the file as
!python file1.py
it works fine, but when using a list I get
for fname in pylist:
!python fname
python: can't open file 'fname': [Errno 2] No such file or directory
so it seems like python is not reading the elements on the list but trying to execute fname. I checked with os.path.abspath(fname) that the paths are correct.
You have a few issues going on with your loop. There is no need to get the len, then look up the index. A for...in... loop will get the actual file string. You can use the subprocess module to make a call.
import subprocess
files = ['./py1.py', './py2.py']
for fname in files:
subprocess.call([fname])
print("done")
the contents of py1 and py2 that I had a hashbang and a print:
#!/usr/bin/env python
# py1.py
print("test 1")
Finally, you can always import the files, but doing this with a list may be challenging.
>>>import py1
test 1

Python cannot find file in current directory with PowerShell

I'm trying to set up a new coding environment for myself so that I may learn Python at home on Windows 10. I already installed Python 2.7 and I am able to run it in PowerShell using the python command. However, when I try to get it to run other files (namely HelloWorld.py) it is getting an error
C:\Python27\python.exe: can't open file 'HelloWorld.py': [Errno 2] No such file or directory
I am in the same folder as HelloWorld.py on PowerShell and when I type ls, it appears on the screen. Most of the other questions I've seen on here were resolved by changing the environment variables in System to include python, but I have already done that. I am looking for anything else to try.

Save Permissions Python

I am writing a program which tracks pay stub information. It runs fine when running it from source code via the terminal and saves files correctly. In order to distribute it to my client, I compiled the code using PyInstaller to create a 1 file .exe for distribution on Windows 7, and then used Advanced Installer 11.4.1 to create a .msi file for them to install on their platform.
My problem is when running the application after installing the .msi. In the package I distributed a .txt file with the data to load and save to.
When attempting to update the file I distributed the following error occurs:
IOError: [Errno 13] Permission denied: 'testSave.txt'
The code I'm using to try and save the file is:
saving = open(file_name, 'w')
saving.write(data)
Is there a way to tell Python 2.7 to write regardless of privileges, or to make this specific file have basic user privileges when installing?
Thanks.
I modified my program to write to a different location using the system environment and telling Advanced Installer to install the .txt in the Local App Data directory. This allowed me to open the file with 'rw' privileges.
path = os.environ.get('LOCALAPPDATA')
path = path.split('\\')
real_path = ''
print path
for dir in path:
real_path += dir + '/'
print real_path
real_path = os.path.normpath(real_path + 'PayTrakker/testSave.txt')
With Advanced Installer you can set permissions, but you need a licensed edition, i.e. Professional or higher.
You can also build a Professional project in the trial period, to test the permissions support and see if it will work for you.

How do I run my Python program that I wrote?

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

python: can't open file 'setup.py': [Errno 2] No such file or directory

I am new in the world of programming, as well as to Ubuntu and Python. I am currently trying to access my setup.py module in the terminal. As of right now, the error it gives me is this:
python: can't open file 'setup.py': [Errno 2] No such file or directory
Honestly, I am not sure how to get this module or where to get this module. I have it on Windows, and it seems to work just fine. I have assumed that since python was already installed on Ubuntu, setup.py would already be here. I think I have assumed wrong.
Any help on this issue would be fantastic. If I need to supply more information, please let me know. :)