0
$ cat a.py
'/t.py'
$ cat /t.py
print 'hello world'; 

What I did is here :

$ chmod +x a.py  # made a.py executable
$ ./a.py         # and run it

This caused my PC to freeze. What have I done wrong?

sigdelsanjog
  • 528
  • 1
  • 11
  • 30
  • you computer did *not* hang because you included a location of a file in your python script. show us your code. – shx2 Aug 03 '14 at 17:51
  • PadraicCunningham t.py is a simple python test file name @shx2 root@algosig:/usr/bin# cat a.py '/t.py' root@algosig:/usr/bin# cat /t.py print 'hello horld'; What i did is here : root@algosig:/usr/bin# chmod +x a.py made a.py executable root@algosig:/usr/bin# ./a.py and run it afterwards terminal aswell as pc got frozen – sigdelsanjog Aug 03 '14 at 17:57
  • You need a [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)). Also, please edit your post instead of adding a comment so we can read your code. – Ry- Aug 03 '14 at 17:58
  • Please edit your Question with the code. – Yogeesh Seralathan Aug 03 '14 at 17:59
  • Oh, and writing `'/t.py'` in Python doesn’t do anything. Do you need a shell script? – Ry- Aug 03 '14 at 18:29

1 Answers1

1

A couple of observations:

  1. Without a shebang (e.g. #!/usr/bin/env python at the top of each script, your code will not be interpreted as python unless you do something like python a.py. If this was intended, you should change the name to something more useful, like a.sh for a shell script.
  2. If your file a.py was interpreted as python, it would do nothing. The string literal '/t.py' by itself doesn't do anything useful. If you want to run the file /t.py, then you have a couple of options, which are discussed in some depth here. One of the simplest would be to use execfile.

Changing a.py to the following should do what you want:

#!/usr/bin/env python
execfile('/t.py')

As an aside, you really shouldn't be logged into your box as the root user and it's generally not considered good housekeeping to put files at the root of your filesystem.

I'm really not sure why what you were doing originally was causing your computer to hang.

Community
  • 1
  • 1
Tom Fenech
  • 72,334
  • 12
  • 107
  • 141