7

I have the following Python script inside a directory called 'test' on my Linux desktop:

#!/usr/bin/python

f = open('test.txt','w')
f.write('testing the script')

So it's /Home/Desktop/test/script.py

If I go inside the directory and type ./script.py it works fine and creates the test.txt file.

However for some reason I am not able to run the script from the Desktop (/Home/Desktop). I tried ./test/script.py, for instance, but didn't work.

The file permissions on the script are 755, and on the directory 777.

Any help would be appreciated.

3
  • Could you provide the error message you're seeing on the console? Commented Oct 8, 2012 at 23:56
  • No error message, so I guess the script is executing but not being able to create the file. Commented Oct 9, 2012 at 0:00
  • If it weren't able to create the file, it should throw an I/O error. I'm guessing it is creating the file, just not where you expect it. Also, as mentioned in my answer, 'w' mode truncates the file, so you won't see any changes if the file exists (except modified time). Commented Oct 9, 2012 at 0:03

5 Answers 5

19

You can use os.path.dirname() and __file__ to get absolute paths like this:

#!/usr/bin/python

import os  # We need this module

# Get path of the current dir, then use it to create paths:
CURRENT_DIR = os.path.dirname(__file__)
file_path = os.path.join(CURRENT_DIR, 'test.txt')

# Then work using the absolute paths:
f = open(file_path,'w')
f.write('testing the script')

This way the script will work on files placed only in the same directory as the script, regardless of the place from which you execute it.

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

2 Comments

Worked like a charm. Thanks a lot man. I am guessing without that change the script was being executed, but it was not being able to create the file cause it was trying to place it at /Desktop instead of /Desktop/test, correct?
@DanielS: Exactly! :) I am happy I helped.
2

If your cwd is /Desktop/test, and then you try to execute ./test/script.py, you're trying to run a script at /Desktop/test/test/script.py. More likely, you just wanted to do ./script.py.

As an aside, your question would have been more useful if you had provided the error message you got from the command line, rather than just saying "didn't work"

If the script is running and nothing is echoed to the console, most likely it's working. Note that opening a file in 'w' mode truncates the file. Maybe you want to use a+?

3 Comments

No error message, which I now realize must mean the script is executing, but the file is not being opened/created, right?
@DanielS sounds like it - might be worth putting in a print statement just for a sanity check . . .does test.txt exist and what are it's permissions?
I'll explore more to see if I discover what's going on.
1

In your open('test.txt', 'w') put open(r'./test.txt', 'w'). When you run it, use "python script.py. See if that works.

4 Comments

You mean use python ./test/script.py? Cause as I said I am trying to run the script from outside its directory.
Yes. I figured that you could generalize the parameters to the python executable.
Gotcha. Do you also mean open(r'./test.txt','w') or open('./test.txt','w'), without the 'r').
No, I meant the r, it's useful for not having to explicitly delimit strings (especially those containing paths). See: tutorialspoint.com/python/python_strings.htm
0

"And so on" doesn't meant much.

Where in the file-system are you? What is the test directories relative position to your location?

Have you tried a fully qualified path? E.g.,

/home/daniel/test/script.py

1 Comment

I am on the Desktop, and 'test' directory is inside it. I edited the question to contain this info. Thanks
0

What directory are you executing in? You might try using:

import os

print os.getcwd()

to verify that the working directory is what you think it is.

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.