Notepad++ can't open files sepearately for python - python

I have recently started using notepad++ for my coding in various different programming languages. In Python specifically, I have tried to open a basic text file (.txt), and print the text inside the file. If I run this file via just double clicking it (in Windows Explorer, not in Notepad++), it will run, and do exactly what I want it to do; print the contents of the text file. However, if I try and run it directly from Notepad++ with the line
C:\Python25\python.exe "$(FULL_CURRENT_PATH)"
then it comes up with an error telling me 'it cannot find the file specified'.
I am under the suspicion that it is using its own directory to try and find the files, so if that is the case is it possible to change it to the files directory?

C:\Python25\python.exe probably isnt your path to python ... I think you just copied it from somewhere
you should probably put the path to your python installation instead (I would guess its something like C:\Python27\python.exe

Related

How do force or change my terminal to run python executable file in python 3 just by opening the file?

My terminal is running python 2 so when I run my python file the program fails.
So basically I am new to programming and trying to create a small python script to help me auto create folders. I also want to give the script to colleges so that they can use it on their systems too.
I know that I can run my file in terminal by using "python3 myfile.py" and it will work, but that's too much off a mission to do for my colleges and as my colleges are not familiar with code or terminal for that matter, I wanted to create an executable file so that they just click to open type a few answers to the promoted question and boom folders created.
This is where I run into a problem, I have "#!/usr/bin/env python3" at the top of my file but when I run the script IDLE opens up and it just shows the code I have written but doesn't seem to run the actual script I wrote. Am I doing something wrong?
I also then though perhaps I could just use the terminal to run the file as it is now executable, so I go into terminal and enter "myfile.py" and the program runs but in python 2 so my script fails as it is in python3. So another question would be is there a way to code into my python file, when running this file make sure you use python3? as I would want this to work on all colleges system without them having to write out anything in terminal?
Sorry for the long explanation but any advice would be greatly appreciated.
Thank you in advance
When you are on windows you can just create a .bat file where you write: python3 myfile.py in it.
Both files have to be in the same directory.
If you want to have a .exe you can also use py2exe.
You could also try just #!/usr/bin/python3 without env.
More here.

Naming a new file in PyCharm

I just started learning python. Up until now, I primarly worked with jupyter notebooks, but now wanted to try out some IDEs. So I tried out PyCharm.
So I started creating some files in a project and started trying out the commands and stuff and there is one thing which just drives me crazy, cause I can not seem to find an answer.
Everytime I create a new python file and name it something like "simple_message", it just saves as a normal file and I can't run any code in it. As soon as I remove the underscore, the file convention changes to an actual python file and I can run the code written in it. So I thought it might be the underscore that makes trouble, so I tried creating other files with underscores but all of them worked. Can someone enlighten me, why does the file name "simple_message" not work as a python file?
Thank you!
From this To this
For me I have to right click on a directory (folder) and select "New" → "Python File" to make a python file. If I go "New" → "File" it will create a text document. So long as you click the correct option you shouldn't have to worry about typing the '.py' or '.txt'
Alternatively I can right click and select "New" → "File" and then name it "simple_message.py" and it will save as a python file correctly, but I must include the .py using this method
Currently PyCharm context menu lets you choose the file type to create.
If you choose "file" you MUST provide the extension .py
If You choose "Python File" pycharm handles the extension for You.
You may need to right click in the text of your .py file and manually choose run from the menu.
Pycharm doesn’t automatically reindex when you create a new .py file, and so it probably just doesn’t know what you want it to run when you hit the green play button.
If you’re saying your python interpreter won’t run a .py file because it has an underscore in the name, that’s just not possible unless you have a modded interpreter or have done something really strange to your OS and I’m guessing you haven’t done either.
Update: it sounds like you don’t have the .py file extension on your file. This will not run for reasons that have nothing to do with Pycharm. Pycharm may “bridge the gap” for you automatically in some situations, but no reason to rely on that.

Python os.chdir() not changing directory

So, I am following a simple tutorial "Python Tutorial: Automate Parsing and Renaming of Multiple Files" and am already encountering a problem where os.chdir() is not working. I am on a Windows 10 system, running python 3.6, and I have tried using both my regular terminal (which has cygwin installed) and bash on ubuntu on Windows.
Here is the code:
import os
print(os.getcwd())
os.chdir('c:/Users/Michelle Kaiser/Desktop/Lab_Progs/PI3Kalpha')
print(os.getcwd())
Here is the reg terminal:
C:\Users\Michelle Kaiser\Desktop\Lab_Progs>python rename.py
C:\Users\Michelle Kaiser\Desktop\Lab_Progs
C:\Users\Michelle Kaiser\Desktop\Lab_Progs>`
The path that it is returning corresponds to the folder my program is located in. I have moved the program 3 times to verify this. Also, it's obviously returning a path only once, so it's probably not responding to the 2 print statements.
Here is the bash terminal:
mkaiser#ZIPPY:/mnt/c/Users/Michelle Kaiser/Desktop/Lab_Progs$ python rename.py
/mnt/c/Users/Michelle Kaiser/Desktop/Lab_Progs
mkaiser#ZIPPY:/mnt/c/Users/Michelle Kaiser/Desktop/Lab_Progs$
I also tried running the code with os.path.exists(), which did not change the output on either terminal. I have definitely double checked that I am saving my program file from one test to the next. Thanks.
I've been trying to change a file that has no whitespace.
It seems like this person has a similar problem:
Python reading whitespace-separated file lines as separate lines

Run .py when .txt is opened?

This feels like a simple question, but I don't know if there's a simple answer. I basically just need to make a .py file run when I open up a .txt. I don't really want a loop to check and then run it, preferably i'd like to bundle the txt and the py so all the user sees is a .txt file. Thanks!
EDIT: More Specifically, is there some type of program that can bundle the txt and py so when they double click the txt it runs the py as well as opening the txt file?
This is not as simple as it seems to be. For running a script when a file is being opened you somehow need to recognize the open file event, and I don't know if this would be possible, especially in Windows.
If you want to do something when the file is being modified (saved), you could give the watchdog package a try.
If this does not help, and you know what program is being used for opening the file, you could do it manually: Write a script which first does whatever you need to have done, then opens the text file using subprocess.call.

Having trouble running python in cmd.

I am a complete noob when it comes to programming. I just downloaded python and I will be using Notepad ++. I have saved a file to my desktop and the file name is test and changed the extension from .txt to .py
So when I go to Notepad ++ and create a program and save it, I go to the cmd prompt making sure I am in my desktop directory and type the following
python test.py
and it tells me that python is not recognized. Any help to fix this problem would be greatly appreciated.
First thing python is indent oriented programing language and it comes with its default editor called IDLE. So if you use notepad++ instead of IDLE it might gives you a syntax error. Second thing for executing python file from command prompt,you need to setup environment variable.Please see below link for setting up environment variable.
https://docs.python.org/2/using/windows.html
You can directly run your program in IDLE without using command prompt. So i would suggest you to use pythons built in editor (IDLE)

Categories

Resources