1

I have a python script that accepts file names as arguments and performs operation on those files. Currently the script is in the same directory as the files it operates on. But I want to be able to run my python script from anywhere in the operating system. When I give the file name as argument to the script, it should know exactly which directory to look for regardless of where the python script is running in the system. How can I achieve this in python?

Here is the relevant portion of the script. Can I just hard code the path? The type_mapping_file will be the filename passed into the script as an argument.

data = None
with open (type_mapping_file, "r") as myfile:
    data=myfile.read()

if(data is not None):
    request_params = {'type': type_name, 'index': index_name}

    send_http_request("/index/type/create", request_params, base_url, post_body=data)

Thank you in advance.

hpaulj
  • 175,871
  • 13
  • 170
  • 282
Horse Voice
  • 7,218
  • 14
  • 56
  • 111

3 Answers3

1

how should it know that?

if it's a constant path you could hardcode it into the script and use os.path to build the full path:

import os
import sys

BASE_PATH = '/my/const/path'

fullpath = os.path.join(BASE_PATH,sys.argv[1])

although, as stated in comments, a more flexible solution would be to pass the path as a separate argument, or pass absolute path for each file

yurib
  • 7,591
  • 3
  • 27
  • 53
1

I assume you're looking for a way to find the path that's given via a relative path, perhaps calling the script itself from another location.

$ cat print_path.py
import os
import sys

print os.path.abspath(sys.argv[1])

If you call this script on a relative path, it prints the absolute path, so you can find it on the disk. The magic is from os.path.abspath().

$ python print_path.py ./planning.md
/Users/Gijs/Desktop/planning.md
$ cd ..

Also if you're calling from somewhere else.

$ python Desktop/print_path.py Desktop/planning.md
/Users/Gijs/Desktop/planning.md

Edit

No, wait, you're looking for the location of the python file that's executing. That's in __file__, see How do I get the path and name of the file that is currently executing?.

Community
  • 1
  • 1
Gijs
  • 8,130
  • 3
  • 24
  • 33
0

The full path to the current file is a magic variable called __file__. This is a suitable argument to functions like os.path.dirname():

print("This Python file is in " + os.path.dirname(__file__))

You can then os.path.join() this with the argument supplied on the command line to get files in the same directory as the script, wherever that may be.

Note that using __file__ in this fashion is not ideal for reusable packages. It is preferred to use pkgutil.get_data() or the equivalent Setuptools function. Basically, you pass it a module or package name (typically __name__, the name of the current module, or __package__, the name of the package you're in) and it retrieves the files for you. This is required if your module is going into an importable zip file or somewhere else where __file__ doesn't exist or have a useful value.

Kevin
  • 25,251
  • 7
  • 54
  • 78