0

I just started learning python for the online course provided by 10gen education, MongoDB for developers (python), i have installed python 2.7 successfully and set the environment vars(path: C:\python27), i can open the python shell and execute the commands and stuff but when i created a python script file named (test.py) and open the command promt, navigated to the current directory C:\10gen and when i try to run it using python test.py it shows me an error

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

however if i execute the same obove command with .txt extension like python test.py.txt it works fine. my question is how would i make filename.py execute from command line .

PS:, my script file contains simple function which iterates over an list and prints it.

Thanks in advance

3 Answers 3

5

This has happened because you used Notepad and tried to save the file as (.txt) TextFile option in Save menu.

use double quotes around "file.py" or select All Files from the dropdown(of Save as type) in the Save As Dialog box.

enter image description here

I recommend using EMacs for Python development. You can also start off with IDLE :-)

Sign up to request clarification or add additional context in comments.

2 Comments

Awesome, thanks Aniket, that solved it. i must say it was indeed such a silly thing, thanks a ton :)
I'll add that Sublime Text is great for python development (not for everyone), with lots of keyboard shortcuts stolen from Emacs.
2

Are you using Notepad to write your python scripts? If so, by default it will add .txt to the end of your saved files. Be sure to click "Save as type All files" in the save dialog when you save your code in Notepad.

7 Comments

In this case the answer would be DO NOT USE NOTEPAD FOR PROGRAMMING. Yes, in caps because using notepad is really that bad. Oh, and if any online course suggests you to use notepad for programming you better stop using it at all.
I'd rather stay with a text editor that is targeted for programmers. GVim, Notepad++ or Sublime Text for example. In Python you usually do not need a bloated IDE at all.
@VinayRao I despise Eclipse or other heavy IDEs, stick to simpler text editors for Python development(for example Emacs) - also keep your Python manual and API reference handy while programming and everything will be good in no time.
@ThiefMaster hmm, good to know, i do use notepad++ at work which is pretty good for indentation i must say, i would get notepad++ on my home PC then, as indentation is must for python .. :)
@VinayRao great, enjoy python hacking
|
2

Windows has added a .txt file extension, which it is now hiding from you. Change your folder preferences to let you see file extensions and it will be there. Try using a more programmer-y text editor such as Notepad++ to avoid the problem recurring.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.