Python Forum

Full Version: pathlib hanging
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Its the first time I'm using pathlib. Any reference to it seems to hang. Is it something to so with permissions ?
I'm working under my home directory, so there shouldn't be an issue.
Output:
bluefrog@pk:~/sample_code$ ls -ltr total 48 drwxr-xr-x 2 bluefrog bluefrog 4096 May 22 2017 db_release drwxr-xr-x 3 bluefrog bluefrog 4096 Nov 7 2017 bash drwxr-xr-x 3 bluefrog bluefrog 4096 Apr 21 21:28 c++ drwxrwxr-x 28 bluefrog bluefrog 4096 Sep 23 12:10 python drwxr-xr-x 3 bluefrog bluefrog 4096 Sep 23 15:34 python_tests drwxr-xr-x 5 bluefrog bluefrog 4096 Sep 23 15:55 pthon_docs bluefrog@pk:~/sample_code$ whoami bluefrog
The code is quite simple:
import pathlib
print("Present working directory : {}".format(pathlib.Path.cwd()))
rootdir = pathlib.Path("/home/bluefrog/sample_code/python")
for path in rootdir.glob("./*.py"):
    print(path, path.stat().st_size, path.parent)
Any ideas ?
if /home/bluefrog/sample_code/python is your source directory, and the program you show is in that path, you can use:
rootdir = Path('.')
here's some stuff you can do:
import os
from pathlib import Path

#make sure you're in source directory:
os.chdir(os.path.dirname(__file__))
rootdir = Path('.')

# to  get your python files:
pyfiles = [filename for filename in rootdir.iterdir() if filename.suffix == '.py']
# some stuff with first file
filename = pyfiles[0]
print(f'fullpath: {filename.resolve()}')
print(f'program name: {filename.name}')
print(f'path components: {filename.parts}')
# show code of first file:
print('\n\n\n')
with filename.open() as fp:
    for line in fp:
        print(line)
I am not sure that it's an issue, but I would replace "./*.py" with '*.py'. ./ is redundant

PS I think you need rglob or glob(*/*.py')