2

I'm making a compress script for my text editor, and it's all working up to the part where it needs to make the file Run. Inside of Run is just this code: python ./App.pyc. When I run the program by double-clicking on it in Finder, it says that it can't open file './App.pyc' [Errno 2] No such file or directory within Terminal.

And if I run it through Terminal after I've cd'd to the directory Run and App.pyc are in, it works. I'm assuming this is because we aren't in the right directory.

My question is, how can I make sure Run is being ran in the right directory? If I put cd in it, it'll work, but then if somebody moves the folder elsewhere it won't work anymore.


#!/usr/bin/python

### Compresser script.

# Compress files.
import App
import Colors

# Import modules
import os

# Clear the folder to put the compressed
# files in (if it exists).
try:
    os.system('rm -rf BasicEdit\ Compressed')
except:
    pass

# Remake the folder to put compressed files in.
os.system('mkdir BasicEdit\ Compressed')

# Move the compiled files into the BasicEdit
# Compressed folder.
os.system('mv App.pyc BasicEdit\ Compressed/')
os.system('mv Colors.pyc BasicEdit\ Compressed/')

# Create contents of run file.
run_file_contents = "python ./App.pyc\n"

# Write run file.
run_file = open("./BasicEdit Compressed/Run", 'w')
run_file.write(run_file_contents)

# Give permissions of run file to anybody.
os.system('chmod a+x ./BasicEdit\ Compressed/Run')

# Finally compress BasicEdit, and remove the old
# folder for BasicEdit Compressed.
os.system('zip -9r BasicEdit.zip BasicEdit\ Compressed')
os.system('rm -rf BasicEdit\ Compressed')

(PS, what's [Errno 1]? I've never seen it before.)

11
  • can you post your script code please? Commented Dec 19, 2014 at 13:23
  • Sure, I'll add the compress script. Commented Dec 19, 2014 at 13:23
  • Use a full path instead of a relative path. Commented Dec 19, 2014 at 13:24
  • 1
    "Errno 2" is referring to the fact that the internal variable errno was assigned a value of 2 by fopen. It is a convention by which the called function communicates the reason for the failure to the caller. It is redundant in the error message and really should not be there. Commented Dec 19, 2014 at 13:26
  • 1
    You could get the absolute path by pwd in bash or os.pwd() in python... Commented Dec 19, 2014 at 13:37

2 Answers 2

3

The Python script's current working directory can be modified with the os.chdir() call, after which references to . will be correct.

If you want to find the location of the source file currently being run rather than hardcoding a directory, you can use:

os.chdir(os.path.dirname(__file__))

The bash equivalent to this logic is:

cd "${BASH_SOURCE%/*}" || {
  echo "Unable to change directory to ${BASH_SOURCE%/*}" >&2
  exit 1
}

See BashFAQ #28 for more details and caveats.

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

6 Comments

Which would probably work, but I'm trying to make a Bash script, not a Python script. :P
Why are you posting only a Python script in your question if you want help with bash?
Because I'm using the Python script to make the standalone Bash file.
Very well; bash version of that logic added.
Note that for this to work reliably, your script needs to start with #!/bin/bash, not #!/bin/sh, as it relies on bash-only extensions.
|
0

As developed above together with @William Purcell, you have to retrieve the absolute path by os.pwd() and then use the absolute path for the python call.

I withdraw my proposal and go with @Charles Duffy's answer. However, I don't delete my attempt as the comments seem to be useful to others!

2 Comments

Well, still not working: OUTPUT = "$(pwd)" echo "dir = ${OUTPUT}" echo "run = ${OUTPUT}/Run" python "${OUTPUT}/Run" But that makes: dir = /Users/towerofnix run = /Users/towerofnix/Run (Can't find directory)
@AquatheSeaWing, FYI, it's much more efficient to run output=$PWD rather than output=$(pwd) in bash. Also, consider using lower-case variable names for things which are neither built-ins or environment variables -- that way you don't overwrite a builtin or an environment variable by mistake.

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.