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.
-
what OS? The answer will vary based on what path to the desktop the filesystem uses.Wooble– Wooble2011-05-10 17:05:07 +00:00Commented 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?Bryce Siedschlaw– Bryce Siedschlaw2011-05-10 17:05:25 +00:00Commented May 10, 2011 at 17:05
-
Windows Operating systemHassan– Hassan2011-05-10 17:05:45 +00:00Commented May 10, 2011 at 17:05
4 Answers
f = open(os.path.join(os.environ['USERPROFILE'], 'DESKTOP', my_filename))
1 Comment
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