home.social

#pathlib — Public Fediverse posts

Live and recent posts from across the Fediverse tagged #pathlib, aggregated by home.social.

  1. Now this is an interesting #Python problem. I don't know if it's a #bug, but it's a change in behaviour that I don't see documented.

    I upgraded from #Debian 12/Bookworm to 13/Trixie, so the default Python3 changed from 3.11 to 3.13. A script of mine broke, because `pathlib.Path.is_mount()` changed behaviour when the path is a symlink (at least to a directory).

    i.e. I'm testing a path that is a symlink. The symlink points to a directory. That directory *is* a mountpoint. The `.is_mount()` test in 3.11 returned True, while in 3.13 it returns False.

    This seems wrong to me. Most path-manipulation functions transparently treat symlinks as if they were the pointed-to object unless you pass an option/flag specifically to say you want the symlink itself.

    Gonna have to dig to see what else I can find.

    #pathlib #path #is_mount #stdlib #behaviour #symlink #filesystem #mountpoint #mount

  2. Now this is an interesting #Python problem. I don't know if it's a #bug, but it's a change in behaviour that I don't see documented.

    I upgraded from #Debian 12/Bookworm to 13/Trixie, so the default Python3 changed from 3.11 to 3.13. A script of mine broke, because `pathlib.Path.is_mount()` changed behaviour when the path is a symlink (at least to a directory).

    i.e. I'm testing a path that is a symlink. The symlink points to a directory. That directory *is* a mountpoint. The `.is_mount()` test in 3.11 returned True, while in 3.13 it returns False.

    This seems wrong to me. Most path-manipulation functions transparently treat symlinks as if they were the pointed-to object unless you pass an option/flag specifically to say you want the symlink itself.

    Gonna have to dig to see what else I can find.

    #pathlib #path #is_mount #stdlib #behaviour #symlink #filesystem #mountpoint #mount

  3. Now this is an interesting #Python problem. I don't know if it's a #bug, but it's a change in behaviour that I don't see documented.

    I upgraded from #Debian 12/Bookworm to 13/Trixie, so the default Python3 changed from 3.11 to 3.13. A script of mine broke, because `pathlib.Path.is_mount()` changed behaviour when the path is a symlink (at least to a directory).

    i.e. I'm testing a path that is a symlink. The symlink points to a directory. That directory *is* a mountpoint. The `.is_mount()` test in 3.11 returned True, while in 3.13 it returns False.

    This seems wrong to me. Most path-manipulation functions transparently treat symlinks as if they were the pointed-to object unless you pass an option/flag specifically to say you want the symlink itself.

    Gonna have to dig to see what else I can find.

    #pathlib #path #is_mount #stdlib #behaviour #symlink #filesystem #mountpoint #mount

  4. Now this is an interesting #Python problem. I don't know if it's a #bug, but it's a change in behaviour that I don't see documented.

    I upgraded from #Debian 12/Bookworm to 13/Trixie, so the default Python3 changed from 3.11 to 3.13. A script of mine broke, because `pathlib.Path.is_mount()` changed behaviour when the path is a symlink (at least to a directory).

    i.e. I'm testing a path that is a symlink. The symlink points to a directory. That directory *is* a mountpoint. The `.is_mount()` test in 3.11 returned True, while in 3.13 it returns False.

    This seems wrong to me. Most path-manipulation functions transparently treat symlinks as if they were the pointed-to object unless you pass an option/flag specifically to say you want the symlink itself.

    Gonna have to dig to see what else I can find.

    #pathlib #path #is_mount #stdlib #behaviour #symlink #filesystem #mountpoint #mount

  5. Now this is an interesting #Python problem. I don't know if it's a #bug, but it's a change in behaviour that I don't see documented.

    I upgraded from #Debian 12/Bookworm to 13/Trixie, so the default Python3 changed from 3.11 to 3.13. A script of mine broke, because `pathlib.Path.is_mount()` changed behaviour when the path is a symlink (at least to a directory).

    i.e. I'm testing a path that is a symlink. The symlink points to a directory. That directory *is* a mountpoint. The `.is_mount()` test in 3.11 returned True, while in 3.13 it returns False.

    This seems wrong to me. Most path-manipulation functions transparently treat symlinks as if they were the pointed-to object unless you pass an option/flag specifically to say you want the symlink itself.

    Gonna have to dig to see what else I can find.

    #pathlib #path #is_mount #stdlib #behaviour #symlink #filesystem #mountpoint #mount

  6. 🐍 Python + Windows + Lecteurs mappés = 💥

    Le bug qui m'a pris 4 versions

    ❌ Path.mkdir() → G:\\
    ✅ os.makedirs() → G:\

    Sur Windows, pathlib crée un double backslash
    avec les lecteurs (G:, H:, ...)

    Debug story complète 👉 syslibre.fr/blog/migration-rob

    #Python #Windows #pathlib

  7. Using #Python's #pathlib to compare two repos and get back some missing files from a "recovered" version of a repo (mostly stuff in .gitignore that is handy not to discard right now).

    from pathlib import Path

    a = Path('sketch-a-day')
    b = Path('sketch-a-day_broken')

    files_a = {p.relative_to(a) for p in a.rglob('*')
    if '.git' not in str(p)
    if 'cache' not in str(p)
    if 'checkpoint' not in str(p)
    }
    files_b = {p.relative_to(b) for p in b.rglob('*')
    if '.git' not in str(p)
    if 'cache' not in str(p)
    if 'checkpoint' not in str(p)
    }
    missing = files_b - files_a

    for p in missing:
    (b / p).rename((a / p))
  8. Изучаем Python: модуль pathlib для начинающих с домашним заданием

    Забудьте о ручном склеивании строк: с pathlib пути элегантно конструируются с помощью оператора /. Проверка существования, чтение, получение родительской директории — всё это становится методами и атрибутами самого объекта. В результате код получается не просто чище и читабельнее, он становится более надежным и по-настоящему "питоничным" (Pythonic).

    habr.com/ru/articles/960440/

    #pathlib #python #python_для_начинающих #python_для_школьников

  9. Today's post is on #Python specifically on the os and pathlib how to open and save files. I'm learning as I write so be kind :) #os #pathlib #files #paths #python #blog Post: www.spsanderson.com/steveondata/...

    Reading and Writing Files in P...

  10. The wonders of #Python on #Windows (via #MSYS2)...

    If you run your Python script using "./script.py", then `Path` from #pathlib becomes `PosixPath`.

    If you run the same script using "python script.py" instead, then `Path` becomes `WindowsPath`.

    Chers!

  11. So I mentioned that my project was at the point of being ready to demo…

    It kinda is (after fixing some leftover bugs this morning/noon), but there is one thing I still have to be care of because it was functionally a directory traversal vulnerability (not super critical, given the use-case, but then again really not something that should be shipped, even as an MVP), so I’m still working on that…

    That said: Python pathlib, WTF is wrong with you for not supporting preventing this
    much better‽ Where are methods like .contains_double_dots(), .remove_double_dots(), … and all of those things‽
    And no,
    .resolve() is not an adequate substitute for so many reasons.

    I’m calling it now: A
    ton of python projects using pathlib are vulnerable to directory-traversal.

    Seriously, this is an issue and I’m shocked that I have never heard anyone bringing it up anywhere!

    #python #pathlib #itsec

  12. I like being able to quickly rename files so that the stem (the part without the suffix/extension) matches the parent folder's name. This is handy when working on my sketch-a-day project that has folders containing images with the ISO YYYY-MM-DDDD dates on their names just like the folders, I then get to use other scripts to post automatically the images. I love that on XFCE's Thunar I can select a file and call my renaming script from a contextual menu!

    github.com/villares/sketch-a-d

    #Python #pathlib #CreativeCoding #XFCE #Thunar

  13. Python's pathlib module - @treyhunner:
    ➡️ pathlib simplifies file path operations in Python.
    ➡️ It offers methods for reading, writing, and manipulating paths.
    ➡️ pathlib ensures cross-platform compatibility.
    ➡️ It replaces older modules like os.path and glob.
    ➡️ Use pathlib for more readable and maintainable code.

    pythonmorsels.com/pathlib-modu

  14. In Python, instead of this:

    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

    I prefer this:

    BASE_DIR = pathlib.Path(__file__).resolve().parent.parent

    For more on the magic of Python's pathlib module, check out the article I published last week: pym.dev/pathlib-module

    #Python #pathlib

  15. I've updated my new pathlib article with additional examples, improved wording, and more links to documentation. ✨

    One nice thing about finally publishing in text form: folks happily provide (frequently) constructive feedback so I can immediately iterate. 💗

    pym.dev/pathlib-module/

    #Python #pathlib

  16. I've finally written a pathlib cheat sheet & sales pitch combination post. ✨

    Another many-year-old to-do list item done. ✅

    If you've been looking for a post to link your pathlib-curious/confused coworkers to, I'm hoping this will help. 🔗

    pym.dev/pathlib-module/

    #Python #pathlib

  17. Looking at it first time today. (svg so responds to [ Ctrl + '+' ] didnt think to zoom it before)

    Wow! percentages on everything.

    I'm amazed! Astounded truly. 140 lines to achieve all of that? And I'm sure I'm doing it all sorts of wrong. But that's a genuine task solved w/ Python in basically a day, but i've been tweeking around w/ the filesystem stuff.

    On the data-science bandwagon of course.
    Thought I'd re-do my ⇨ *OLD* ⇦ php static cms (did i meantion it's old?) as a python app to learn from that perspective.
    see:
    statecollegeguitarlessons.com/

    I dunno why i want to start by learning how to mess with the filesystem. probably some kind of psychosis. someone has a name for that.

    traverse the dirs w/ tuple os.walk i think at that time, abandoned for pathlib - thought, okay: this is pretty easy w/ python! let's try counting file-types, etc. clearly abandoning the cms idea by that time. Also, i see Jupyter Notebook is basically already the thing. so. ha!

    #Python #Amazed #Pandas #Plotly #pathlib #newb

    @python_discussions
    @diazona
    @ketmorco

  18. Looking at it first time today. (svg so responds to [ Ctrl + '+' ] didnt think to zoom it before)

    Wow! percentages on everything.

    I'm amazed! Astounded truly. 140 lines to achieve all of that? And I'm sure I'm doing it all sorts of wrong. But that's a genuine task solved w/ Python in basically a day, but i've been tweeking around w/ the filesystem stuff.

    On the data-science bandwagon of course.
    Thought I'd re-do my ⇨ *OLD* ⇦ php static cms (did i meantion it's old?) as a python app to learn from that perspective.
    see:
    statecollegeguitarlessons.com/

    I dunno why i want to start by learning how to mess with the filesystem. probably some kind of psychosis. someone has a name for that.

    traverse the dirs w/ tuple os.walk i think at that time, abandoned for pathlib - thought, okay: this is pretty easy w/ python! let's try counting file-types, etc. clearly abandoning the cms idea by that time. Also, i see Jupyter Notebook is basically already the thing. so. ha!

    #Python #Amazed #Pandas #Plotly #pathlib #newb

    @python_discussions
    @diazona
    @ketmorco

  19. Looking at it first time today. (svg so responds to [ Ctrl + '+' ] didnt think to zoom it before)

    Wow! percentages on everything.

    I'm amazed! Astounded truly. 140 lines to achieve all of that? And I'm sure I'm doing it all sorts of wrong. But that's a genuine task solved w/ Python in basically a day, but i've been tweeking around w/ the filesystem stuff.

    On the data-science bandwagon of course.
    Thought I'd re-do my ⇨ *OLD* ⇦ php static cms (did i meantion it's old?) as a python app to learn from that perspective.
    see:
    statecollegeguitarlessons.com/

    I dunno why i want to start by learning how to mess with the filesystem. probably some kind of psychosis. someone has a name for that.

    traverse the dirs w/ tuple os.walk i think at that time, abandoned for pathlib - thought, okay: this is pretty easy w/ python! let's try counting file-types, etc. clearly abandoning the cms idea by that time. Also, i see Jupyter Notebook is basically already the thing. so. ha!

    #Python #Amazed #Pandas #Plotly #pathlib #newb

    @python_discussions
    @diazona
    @ketmorco

  20. Looking at it first time today. (svg so responds to [ Ctrl + '+' ] didnt think to zoom it before)

    Wow! percentages on everything.

    I'm amazed! Astounded truly. 140 lines to achieve all of that? And I'm sure I'm doing it all sorts of wrong. But that's a genuine task solved w/ Python in basically a day, but i've been tweeking around w/ the filesystem stuff.

    On the data-science bandwagon of course.
    Thought I'd re-do my ⇨ *OLD* ⇦ php static cms (did i meantion it's old?) as a python app to learn from that perspective.
    see:
    statecollegeguitarlessons.com/

    I dunno why i want to start by learning how to mess with the filesystem. probably some kind of psychosis. someone has a name for that.

    traverse the dirs w/ tuple os.walk i think at that time, abandoned for pathlib - thought, okay: this is pretty easy w/ python! let's try counting file-types, etc. clearly abandoning the cms idea by that time. Also, i see Jupyter Notebook is basically already the thing. so. ha!

    #Python #Amazed #Pandas #Plotly #pathlib #newb

    @python_discussions
    @diazona
    @ketmorco

  21. Looking at it first time today. (svg so responds to [ Ctrl + '+' ] didnt think to zoom it before)

    Wow! percentages on everything.

    I'm amazed! Astounded truly. 140 lines to achieve all of that? And I'm sure I'm doing it all sorts of wrong. But that's a genuine task solved w/ Python in basically a day, but i've been tweeking around w/ the filesystem stuff.

    On the data-science bandwagon of course.
    Thought I'd re-do my ⇨ *OLD* ⇦ php static cms (did i meantion it's old?) as a python app to learn from that perspective.
    see:
    statecollegeguitarlessons.com/

    I dunno why i want to start by learning how to mess with the filesystem. probably some kind of psychosis. someone has a name for that.

    traverse the dirs w/ tuple os.walk i think at that time, abandoned for pathlib - thought, okay: this is pretty easy w/ python! let's try counting file-types, etc. clearly abandoning the cms idea by that time. Also, i see Jupyter Notebook is basically already the thing. so. ha!

    #Python #Amazed #Pandas #Plotly #pathlib #newb

    @python_discussions
    @diazona
    @ketmorco

  22. Last week I moved everything meaningful from my pile of old USB external drives to my two main drives. I ended up with ~150k files to sort through.

    I wasn't sure pathlib would be an effective tool for the job, but it turned out to be fantastic!

    mostlypython.com/how-clean-are

  23. #Python question...

    I recently ran into a surprise using #pathlib.Path - seems to be a sharp edge and I'm curious if others have run into it and what you thought.

    I wanted to find all #files/#dirs below the current #directory, and thought a #recursive #glob would do it - `Path.cwd().glob("**")`. I was surprised that this returns all directories but not files. "**/*" is needed to find the files as well.

    I'm not a big user of recursive globs, so maybe this is expected #behaviour?

  24. Covenantus detectus или ещё одна DS-задача

    Привет, Хабр! Сегодня с вами участник профессионального сообщества NTA Серебренников Дмитрий. И по дружбе, и по IT‑службе регулярно сталкиваюсь с задачами Data Science. Решением одной из них планирую сегодня поделиться. Поработаю с кредитной документацией, выжму из неё необходимое для аудиторской проверки. Из инструментов применю ловкость рук, python, pathlib, regex, pandas и Abbyy Finereader. Итак, задача состояла в получении необходимых сущностей (ковенантов) из разных по формату и содержанию документов. Пост предназначен прежде всего для столкнувшихся с такой задачкой и тех, кто недавно взял курс в науку о данных. Кстати, о данных — все совпадения случайны, исследуемые материалы вымышлены. Covenantus detectus

    habr.com/ru/articles/781514/

    #pdf #распознование_текста #регулярки #регулярные_выражения #распознование_изображений #python #pathlib #regex #Abbyy_FineReader

  25. Covenantus detectus или ещё одна DS-задача

    Привет, Хабр! Сегодня с вами участник профессионального сообщества NTA Серебренников Дмитрий. И по дружбе, и по IT‑службе регулярно сталкиваюсь с задачами Data Science. Решением одной из них планирую сегодня поделиться. Поработаю с кредитной документацией, выжму из неё необходимое для аудиторской проверки. Из инструментов применю ловкость рук, python, pathlib, regex, pandas и Abbyy Finereader. Итак, задача состояла в получении необходимых сущностей (ковенантов) из разных по формату и содержанию документов. Пост предназначен прежде всего для столкнувшихся с такой задачкой и тех, кто недавно взял курс в науку о данных. Кстати, о данных — все совпадения случайны, исследуемые материалы вымышлены. Covenantus detectus

    habr.com/ru/articles/781514/

    #pdf #распознование_текста #регулярки #регулярные_выражения #распознование_изображений #python #pathlib #regex #Abbyy_FineReader

  26. On #Python if you use relative paths on a script, they are interpreted as relative to the "current working directory" Path.cwd(), when you call the script from the command line. If you want for some reason to reach a file relative to the script source itself you can use:

    from pathlib import Path
    file_path = Path(__file__).parent / 'data' / 'my_file.dat'

    #pathlib #paths update: @isagalaev saw a typo, there should be no leading / on data/my_file.dat
    update2: better to let pathlib do the sub-dir thing, also more portable.

  27. #TodayILearned that in #Python's #pathlib.Path "broken links" are not folders, so .is_folder() returns False, but then .is_file() also returns False ...

    docs.python.org/3/library/path

  28. A question for python users: can pathlib access AFP shares ? I'm on a Debian box and afp:// addresses do not work.

    #python #programming #afp #pathlib

  29. @hexylena

    I get not liking the "clever" overloading of the division operator. Luckily, you don't have to use it - you can pass a full path as a single string, or a list of strings, and it will Do The Right Thing:

    >>> Path("/usr", "lib", "grub")
    PosixPath('/usr/lib/grub')

    >>> Path("/usr/lib/grub")
    PosixPath('/usr/lib/grub')

    pathlib is nice because it gives some better, high-level abstractions. Things like `path.relative_to(other_path)` are very natural to read.

    [...]

    #python #pathlib

  30. Day 3,406 of wishing Python's tempfile module returned pathlib.Path objects instead of str.

    #python #pathlib

  31. Python stdlib's `pathlib.Path.with_suffix` is
    utterly broken.