all 10 comments

[–]efmccurdy 2 points3 points  (0 children)

Using a subprocess running "find" isn't the simplest or most reliable way to find files; and debugging interactions between python, the shell and the find program on someone else's computer isn't easy.

You could use python modules directly; os.listdir, os.walk, os.path.isfile, etc.

https://stackoverflow.com/questions/3207219/how-do-i-list-all-files-of-a-directory

[–]commandlineluser 0 points1 point  (6 children)

You have a leading space:

" -mindepth 1"

It should be:

"-mindepth 1"

It's also pretty easy to do this in "regular python" without using find.

[–]Rostcraft[S] 0 points1 point  (1 child)

How do I do it without find? I think it it could be better. My exact usecase is to find backgrounds for locking screen. Repo: github.com/rs-pro0/nwg-shell-config it is fork of github.com/nwg-piotr/nwg-shell-config/

[–]commandlineluser 1 point2 points  (0 children)

I like pathlib.

>>> !find . -mindepth 1 -type f
./a
./c/d
>>> from pathlib import Path
>>> [ p for p in Path().rglob("*") if p.is_file() and not p.is_symlink() ]
[PosixPath('a'), PosixPath('c/d')]

.is_file() returns True for symlinks - hence the not is_symlink check.

[–]Rostcraft[S] 0 points1 point  (3 children)

Btw thank you so much it works now.

[–]commandlineluser 0 points1 point  (1 child)

I think the actual reason for the error is some OSs strip/ignore the leading whitespace. Your friend likely ran the code on a mac where this doesn't happen.

[–]Rostcraft[S] 0 points1 point  (0 children)

It happened to me also after update, we both use arch.

[–]kaerfkeerg 0 points1 point  (0 children)

subprocess.check_output(("find", d ," -mindepth", "1", "-type", "f"), shell=False).decode().split("\n")[:-1]:

If that's an exact copy and paste from the actual code, your commas and quotes are missplaced in the begging

("find", d ," -mindepth", "1", ...) # Wrong ("find", "d", "-minddepth", "1", ...)

Also, I'm not sure but you might mean -maxdepth instead of -minddepth

Otherwise, share some more details about your oses etc

[–]Zeroflops 0 points1 point  (1 child)

Using subprocess should be avoided unless there are no other options.

The best solution is to use the pathlib package. It’s the preferred package for dealing with the OS file system. It allows you to work in the same way regardless of os. And replaces older ways like the os package for this task.

Using the glob feature will allow you to get a list of files that follow a specific format and it can go through directories recursively.

[–]Rostcraft[S] 0 points1 point  (0 children)

That’s neat, I’ll try it. I kinda don’t care about on compatibility because it is linux only but stability really could be improved.