Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pathlib hanging
#1
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 ?
Reply
#2
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)
Reply
#3
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')
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python script is hanging while calling a procedure in database prasanthi417 4 503 Jan-17-2024, 02:33 PM
Last Post: deanhystad
  Pathlib import not working chriswrcg 9 3,669 May-29-2022, 07:37 PM
Last Post: snippsat
  deleting an empty Dir, using pathlib.Path tester_V 9 5,780 Jul-01-2021, 01:53 PM
Last Post: Gribouillis
  Trying to pathlib instead of os.path tester_V 4 2,476 Jun-22-2021, 04:15 AM
Last Post: tester_V
  pathlib destpath.exists() true even file does not exist NaN 9 4,666 Dec-01-2020, 12:43 PM
Last Post: NaN
  Question about if with () or without () / pathlib Tecuma 3 2,209 Apr-02-2020, 10:02 AM
Last Post: Tecuma
  Hanging in View method learn_python 0 1,949 Feb-15-2019, 04:57 AM
Last Post: learn_python
  PyPDF2 Hanging When Trying to Open Corrupted PDF bmccollum 6 8,750 Nov-09-2018, 10:40 AM
Last Post: Larz60+
  pathlib: resolving a path that does not exist Skaperen 6 5,480 Sep-08-2018, 12:25 AM
Last Post: Skaperen
  makin hardlinks with pathlib.Path Skaperen 2 5,222 Sep-06-2018, 07:53 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020