How do I find directory of the Python running script from inside the script

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

How do I find the directory of the running Python script from inside Python [3.3]? http://stackoverflow.com/questions/4934806/python-how-to-find-scripts-directory http://stackoverflow.com/questions/4934806/python-how-to-find-scripts-directory

The closest I have got to the answer is: sys.argv[0] , but that still includes the file name, so I cannot use it. Is there any other way?

NOTE: I am fairly new to Python.

Here is some code I have made so far (the part where it says rundir = sys.argv[0] is where the suggested code will go):

import pygame
from pygame.locals import *
import os, sys
import time
pygame.init()

import statuscheck
print("Completed program status check.")

import mods.modlist
print("Loaded all mods..")

print("Completed loading")

sys.dont_write_bytecode = True

rundir = sys.argv[0]

print("Running from" + rundir)

Answers

http://docs.python.org/2/library/os.html#os.getcwd http://docs.python.org/2/library/os.html#os.getcwd

import os
cwd = os.getcwd()

It works on both Unix and Windows.

Make sure the syntax is correct and that you use it like above.

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/19322836/how-do-i-find-directory-of-the-python-running-script-from-inside-the-script

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils