2

how can i run my program using test files on my desktop without typing in the specific pathname. I just want to be able to type the file name and continue on with my program. Since i want to be able to send it to a friend and not needing for him to change the path rather just read the exact same file that he has on his desktop.

3
  • what OS? The answer will vary based on what path to the desktop the filesystem uses. Commented May 10, 2011 at 17:05
  • Can you give an example? I'm not quite sure what you're looking for. What do you put now and what do you want to put? Commented May 10, 2011 at 17:05
  • Windows Operating system Commented May 10, 2011 at 17:05

4 Answers 4

1

f = open(os.path.join(os.environ['USERPROFILE'], 'DESKTOP', my_filename))

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

1 Comment

Hassan: the USERPROFILE environment variable should be set by Windows; Desktop is, uh, your Desktop directory (as specified in the question)
1

If you are on Unix/Mac you can add a shebang at the top of your script and set it as executable. Usually you'd do:

#!/usr/bin/env python

And then to make it executable, from a terminal use chmod:

chmod +x script.py

Now you can run the script (if you are in the same directory) like:

./script.py

If you are on Windows you'll want to add the Python executable to your %PATH% enviromental variable, then you can run your script with python script.py. They talk about this in more depth in the Python documentation: http://docs.python.org/using/windows.html

Comments

0

If you place your Python script in the same directory as the files your script is going to open, then you don't need to specify any paths. Be sure to allow the Python installer to "Register Extensions", so Python is called when you double-click on a Python script.

Comments

0

You can tell your friend to make *.py files to be executed by the interpreter. Change it from Explorer:Tools:Folder Options:File Types.

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.