Jump to content

I'm kind of noob to python so bare with me guys....I can't figure out how to open python scripts or any file using IDLE in Windows 7...I googled up found that i need to change my envr variable...and I did changed but still same syntax error iis occurring..pls tell me what i need to do to open, copy,etc..files from any directory..

 

 

I'm posting this image to get you an idea...https://docs.google.com/file/d/0B38c6FHZKgdYUzJCUmRob3FvaG8/edit?usp=sharing

 

Link to comment
https://linustechtips.com/topic/37700-open-file-in-python-idlewindows-7/
Share on other sites

Link to post
Share on other sites

Firstly, I have not used the IDLE IDE, so I am not sure what functionalities it has. But, from your screen shot, it appears  you have a python shell open, not an IDE open. So now my question turns into:

 

1. Are you trying to edit a python script?

If so, I believe you can click File->New or FIle->Open to create or edit existing scripts.

 

2. Are  you trying to run the python script?

This can be done a few ways. For example, your IDE allows you to run it from its editor (Run in the Toolbar). You can also run it from a command prompt (NOT python prompt) after changing the directory to where your script.py file is located by typing "python script.py" (assuming the python.exe is on your PATH environment variable).

 

3. Are you trying to import a module that where you have defined constants/functions/classes/etc?

-Make sure that the path to your modules are located in the PYTHONPATH environment variable

-Then just use the "import myModule" statement either at the python command prompt or in other script.py files to import the module named "myModule.py"

 

So, you have a python command prompt running. This is python interpreter itself, and when you type "open("c:\Whatever\your\file\was")" python thinks you are trying to open a file for read/write operations, not for script editing/running.

 

Just as a bit of background to help you understand:

Python is an interpreter. When you write a script, you are telling the interpreter to do things. However, you can bring up a python command prompt as well (as your IDE does) which will allow you to dynamically type things for the python interpreter to run (open, for example). So when you see ">>>" this is python waiting for you to tell it to do something. So lets say your script contained:

print "Hello World!"

You could just type that at the ">>>" prompt and it would spit out "Hello World", and then ">>>" for the next thing you want it to do. A script just puts a lot of things you want the interpreter to parse into one file, for convenience and ease of use.

| CPU: 2600K @ 4.5 GHz 1.325V | MB: ASUS P8Z68-V pro | GPU: EVGA GTX 480 clk 865/mem 2100 | RAM: Corsair Vengeance 1600 MHz CL9 | HDD: Muskin Chronos Deluxe 240GB(win8) && ADATA SX900 120 GB(ubuntu 12.04) | PSU: Seasonic x760 |

Link to post
Share on other sites

I never used python, but from what I have seen of it I could make a few suggestions.

 

The first is

open(C:\Users\.....

needs to have quotes around it

open("C:\Users\....");

 

Second, and someone correct me if I am wrong, \ usually needs to be doubled up in quotes \\ (In c, c++, c# it does because \n becomes new line character so \\ becomes \)

 

Third, if you changed the envirnment variable to C:\Users\HRB|Downlaods\Others\Pyrex does this work?

hello.py

instead of the open stuff (from my googling I think this should work)

0b10111010 10101101 11110000 00001101

Link to post
Share on other sites

I never used python, but from what I have seen of it I could make a few suggestions.

 

The first is

open(C:\Users\.....

needs to have quotes around it

open("C:\Users\....");

 

Second, and someone correct me if I am wrong, \ usually needs to be doubled up in quotes \\ (In c, c++, c# it does because \n becomes new line character so \\ becomes \)

 

Third, if you changed the envirnment variable to C:\Users\HRB|Downlaods\Others\Pyrex does this work?

hello.py

instead of the open stuff (from my googling I think this should work)

 

Correct, on both comments. He needs quotes (single or double will work) and he also needs to specify another parameter ('r', 'w', etc) to specifiy whether he wants to read or write from the file or both. You are correct about the file separator as well. Since he is using windows, the file separator is a backslash which is the escape character. Because of this, you need to escape it, so that it is interpreted as a backslash (so C:\path\here becomes C:\\path\\here). But environment variables are not needed for file I/O.

 

However, the open() function is used for file I/O, and I don't think this is what he wants to do. If you look at his picture, he is trying to open a hello.py file (which is a python script file). I think he is trying to execute this script, not programmatically read/write from/to a this file.

 

So if he set the environment variable PYTHONPATH=C:\Users\HRB|Downlaods\Others\Pyrex

He could call "import hello" from the python prompt to run the script. If he didn't want to set that environment variable, he could just run his script from the command line with "python C:\Users\HRB|Downlaods\Others\Pyrex\hello.py" or he could run it from his IDE, as previously mentioned. 

| CPU: 2600K @ 4.5 GHz 1.325V | MB: ASUS P8Z68-V pro | GPU: EVGA GTX 480 clk 865/mem 2100 | RAM: Corsair Vengeance 1600 MHz CL9 | HDD: Muskin Chronos Deluxe 240GB(win8) && ADATA SX900 120 GB(ubuntu 12.04) | PSU: Seasonic x760 |

Link to post
Share on other sites

I never used python, but from what I have seen of it I could make a few suggestions.

 

The first is

open(C:\Users\.....

needs to have quotes around it

open("C:\Users\....");

 

Second, and someone correct me if I am wrong, \ usually needs to be doubled up in quotes \\ (In c, c++, c# it does because \n becomes new line character so \\ becomes \)

 

Third, if you changed the envirnment variable to C:\Users\HRB|Downlaods\Others\Pyrex does this work?

hello.py

instead of the open stuff (from my googling I think this should work)

thanks bro :) .....I didn't knew about that double backslash thing...that was the main problem....and yeah!! those quotes too..but that was just stupid of me....anyways thanks again man..it was really frustrating...

Link to post
Share on other sites

Firstly, I have not used the IDLE IDE, so I am not sure what functionalities it has. But, from your screen shot, it appears  you have a python shell open, not an IDE open. So now my question turns into:

 

1. Are you trying to edit a python script?

If so, I believe you can click File->New or FIle->Open to create or edit existing scripts.

 

2. Are  you trying to run the python script?

This can be done a few ways. For example, your IDE allows you to run it from its editor (Run in the Toolbar). You can also run it from a command prompt (NOT python prompt) after changing the directory to where your script.py file is located by typing "python script.py" (assuming the python.exe is on your PATH environment variable).

 

3. Are you trying to import a module that where you have defined constants/functions/classes/etc?

-Make sure that the path to your modules are located in the PYTHONPATH environment variable

-Then just use the "import myModule" statement either at the python command prompt or in other script.py files to import the module named "myModule.py"

 

So, you have a python command prompt running. This is python interpreter itself, and when you type "open("c:\Whatever\your\file\was")" python thinks you are trying to open a file for read/write operations, not for script editing/running.

 

Just as a bit of background to help you understand:

Python is an interpreter. When you write a script, you are telling the interpreter to do things. However, you can bring up a python command prompt as well (as your IDE does) which will allow you to dynamically type things for the python interpreter to run (open, for example). So when you see ">>>" this is python waiting for you to tell it to do something. So lets say your script contained:

print "Hello World!"

You could just type that at the ">>>" prompt and it would spit out "Hello World", and then ">>>" for the next thing you want it to do. A script just puts a lot of things you want the interpreter to parse into one file, for convenience and ease of use.

thanks gob..last para did clear some facts for me.. :)

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×