0

I am able to have a list of file names in a list named 'a' using the linux terminal command on Jupiter as follows;

a=!find . -type f \! -name '*.*' -print

To have the files names without extensions, the corresponding command works on linux terminal;

find . -type f -exec sh -c '
    for pathname do
        pathname=$( basename "$pathname" )
        printf "%s\n" "${pathname%.*}"
    done' sh {} +

But how can I run this command on Jupyter assigning it to a name 'a' like above. The below code didn`t work.

a=!find . -type f -exec sh -c '
        for pathname do
            pathname=$( basename "$pathname" )
            printf "%s\n" "${pathname%.*}"
        done' sh {} +

I have tried changing the indentation of lines as well, i get syntax error, or nothing. From my search on the command line of for loop on Jupiter the '!' should be perhaps in a different location. How can I make it work?

  • Define "didn't work". How is it not working as expected or intended? Why are you not using Python's internal file traversal rather than escaping out to a shell? Are you aware that there is no such thing as an "file extension" in a POSIX environment? – DopeGhoti Jan 23 at 22:30
  • thanks. I meant if i preserve to indentation i get invalid syntax for 'pathname do', if i remove the indentations, nothing happens but when i print 'a' i get ''sh: 1: sh: Syntax error: end of file unexpected (expecting "done")''. I am not familiar with Python's internal file traversal or the POSIX environment yet. – kutlus Jan 23 at 22:51
  • You may need to escape the newlines by ending each line of the multi-line quoted content with a backslash, or just deal with it being one ugly line and get rid of the newlines altogether. – DopeGhoti Jan 23 at 22:53
0

I solved this problem in a different way if anyone has a similar issue. I used the command line a=!find . -type f \! -name '*.*' -print

this created a list of filenames with extensions. I removed the extensions from each element.

files = [os.path.splitext(fname)[0] for fname in a]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.