photog.social is one of the many independent Mastodon servers you can use to participate in the fediverse.
🌈 An inclusive place for your photos, silliness, and convos! 🌈

Administered by:

Server stats:

253
active users

#pathlib

0 posts0 participants0 posts today
Steve has ☕️ for brains<p>I’m still a strong believer in PBD - print-based-debugging — but I hate getting stumped for two days because the value I’m printing automatically prints a string, but the value != an actual string so an equality test fails</p><p><a href="https://hachyderm.io/tags/python" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>python</span></a> <a href="https://hachyderm.io/tags/pathlib" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>pathlib</span></a></p>
Mathias Hasselmann<p>The wonders of <a href="https://mastodon.green/tags/Python" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>Python</span></a> on <a href="https://mastodon.green/tags/Windows" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>Windows</span></a> (via <a href="https://mastodon.green/tags/MSYS2" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>MSYS2</span></a>)...</p><p>If you run your Python script using "./script.py", then `Path` from <a href="https://mastodon.green/tags/pathlib" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>pathlib</span></a> becomes `PosixPath`.</p><p>If you run the same script using "python script.py" instead, then `Path` becomes `WindowsPath`.</p><p>Chers!</p>
C.<p><a href="https://mindly.social/tags/Python" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>Python</span></a> question...</p><p>I recently ran into a surprise using <a href="https://mindly.social/tags/pathlib" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>pathlib</span></a>.Path - seems to be a sharp edge and I'm curious if others have run into it and what you thought.</p><p>I wanted to find all <a href="https://mindly.social/tags/files" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>files</span></a>/#dirs below the current <a href="https://mindly.social/tags/directory" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>directory</span></a>, and thought a <a href="https://mindly.social/tags/recursive" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>recursive</span></a> <a href="https://mindly.social/tags/glob" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>glob</span></a> 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.</p><p>I'm not a big user of recursive globs, so maybe this is expected <a href="https://mindly.social/tags/behaviour" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>behaviour</span></a>?</p>
Alexandre B A Villares 🐍<p>On <a href="https://ciberlandia.pt/tags/Python" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>Python</span></a> if you use relative paths on a script, they are interpreted as relative to the "current working directory" <code>Path.cwd()</code>, 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:</p><pre><code>from pathlib import Path<br>file_path = Path(__file__).parent / '/data/my_file.dat'<br></code></pre><p><a href="https://ciberlandia.pt/tags/pathlib" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>pathlib</span></a> <a href="https://ciberlandia.pt/tags/paths" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>paths</span></a></p>
C.<p><span class="h-card"><a href="https://galaxians.garden/users/hexylena" class="u-url mention" rel="nofollow noopener" target="_blank">@<span>hexylena</span></a></span> </p><p>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:</p><p>&gt;&gt;&gt; Path("/usr", "lib", "grub")<br>PosixPath('/usr/lib/grub')</p><p>&gt;&gt;&gt; Path("/usr/lib/grub")<br>PosixPath('/usr/lib/grub')</p><p>pathlib is nice because it gives some better, high-level abstractions. Things like `path.relative_to(other_path)` are very natural to read.</p><p>[...]</p><p><a href="https://mindly.social/tags/python" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>python</span></a> <a href="https://mindly.social/tags/pathlib" class="mention hashtag" rel="nofollow noopener" target="_blank">#<span>pathlib</span></a></p>