2

I keep trying to run a bit of python on mac terminal, and I'm hit with the following error: "TypeError: 'module' object is not callable"

Reference code:

import re
import pathlib as Path
mypath = Path('users/pranav/Desktop/sir/samplenames.txt')

Could someone break down the error for me and explain what I should do to fix it?

I also tried to fix it using sys, however, before I could even work through it I was returned an error stating: AttributeError: 'list' object has no attribute 'dirname'

Code w/ sys:

import sys
print(sys.path.dirname())
import re
import pathlib as Path
mypath = Path('/users/pranav/Desktop/sir/samplenames.txt')

Any help would be greatly appreciated

Jamiu S.
  • 5,257
  • 5
  • 12
  • 34
pvp
  • 23
  • 1
  • 5
  • 2
    it should be `from pathlib import Path` instead of `import pathlib as Path` you are indeed trying to use a module as a function instead of importing a specific class of the module. – Titouan L Jun 22 '22 at 06:47
  • For the second problem: `sys.path`. The value of sys.path is a list, comparable to the `PATH` environment variable (but not exactly, see https://stackoverflow.com/questions/897792/where-is-pythons-sys-path-initialized-from ) . It does not have a `dirname` attribute. Maybe you were thinking of `os.path.dirname` function, as in `os.path.dirname('/users/pranav/Desktop/sir/samplenames.txt')` – qrsngky Jun 22 '22 at 06:52

1 Answers1

1
from pathlib import Path

SCR_DIR = 'C:\\users\\pranav\\Desktop\\sir'

mypath = Path(SRC_DIR, "samplenames.txt")
Jamiu S.
  • 5,257
  • 5
  • 12
  • 34