If you are using an operating system that has Sysfs support for GPIO (e.g. Raspbian), then you can use that to read the pin state from within Python. Note that this method uses BCM pin numbering.
To check the state of pin GPIO24, first you need to export it to userspace. As root, do the shell command
echo 24 > /sys/class/gpio/export
That creates a sysfs directory for the pin at /sys/class/gpio/gpio24/
, containing various files you can read and write to to control the pin. You can access them via a Python script like this:
try:
with open("/sys/class/gpio/gpio24/value") as pin:
status = pin.read(1)
except:
print "Remember to export the pin first!"
status = "Unknown"
print status
This will work whether or not you have set up the pin using RPi.GPIO. However, if you do then use RPi.GPIO or another library to access the pin, you need to be aware that setting up a pin as an input or output may change its state. You will need to experiment!
eLinux.org has lots of useful information about accessing RPi Low-level peripherals and GPIO by different methods. There is also detailed documentation for the Sysfs GPIO interface.