[Tutor] How to call the path of an input file
Steven D'Aprano
steve at pearwood.info
Thu Jan 21 18:18:51 EST 2016
Hi John, and welcome. My responses interleaved between yours below.
On Thu, Jan 21, 2016 at 11:51:09AM -0500, John Villarraga wrote:
> I would like to know what is the syntax to call the path of the input file.
Python only has one syntax for calling anything, and that is to put
parentheses (round brackets) after it, with any arguments needed inside
the parens. So this is how you would call the path:
path()
But that's not going to do you any good, since path is surely going to
be a string, and strings aren't callable: you'll just get a TypeError:
py> path = "directory/file.txt"
py> path()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable
So would you like to clarify what you actually mean by "call the path"?
> Below, my code is calling the input file, but not the path.
>
> Sorry for the inconvenience and thank you for your time.
>
>
> import sys
> path = sys.argv[1]
> y = map(str.lower, path.split())
I *think* what you mean is that you have a python script, let's call it
"script.py", and you run that script like this:
python script.py
and now you want to add a path (a path to what? a file?) as a command
line argument:
python script.py /some/directory/file.txt
And then what is supposed to happen?
I'm going to guess that you want to take the argument given:
path = "/some/directory/file.txt"
and do something to it, not sure what. Perhaps normalise the case?
import os
path = os.path.normcase(path)
should do what you want. You can read up on the functions available in
the os.path module here:
For version 2:
https://docs.python.org/2/library/os.path.html
For version 3:
https://docs.python.org/3/library/os.path.html
--
Steve
More information about the Tutor
mailing list