0

I'm sorry for asking a duplicate as this and this are very similar but with those answers I don't seem to be able to make my code work.

If have a jupyter notebook cell with:

some_folder = "/some/path/to/files/*.gif"
!for name in {some_folder}; do echo $name; done

The output I get is just {folder}

If I do

some_folder = "/some/path/to/files/*.gif"
!for name in "/some/path/to/files/*.gif"; do echo $name; done # <-- gives no newlines between file names
# or 
!for name in /some/path/to/files/*.gif; do echo $name; done # <-- places every filename on its own line

My gif files are printed to screen.

So my question why does it not use my python variable in the for loop? Because the below, without a for loop, does work:

some_folder = "/some/path/to/files/"
!echo {some_folder}

Follow up question: I actually want my variable to just be the folder and add the wildcard only in the for loop. So something like this:

some_folder = "/some/path/to/files/"
!for name in {some_folder}*.gif; do echo $name; done

For context, later I actually want to rename the files in the for loop and not just print them. The files have an extra dot (not the one from the .gif extension) which I would like to remove.

C. Binair
  • 419
  • 4
  • 14

1 Answers1

1

There's an alternative way to use shell bash in a Jupyter cell with cell magic, see here. It seems to allow what you are trying to do.

If you already ran in a normal cell some_folder = r"/some/path/to/files/*.gif" or some_folder = "/some/path/to/files/*.gif", then you can try in a separate cell:

%%bash -s {some_folder}
for i in {1..5}; do echo $1; done

That said, what you seems to be trying to do with some_folder = "/some/path/to/files/*.gif" isn't going to work as such. If you try to pass "/some/path/to/files/*.gif" from Python to bash, it isn't going to work like passing /some/path/to/files/*.gif directly to bash. Bash isn't passing "/some/path/to/files/*.gif" directly to a command, it expands it and then passes it. There's not going to be an expansion passing from Python. And there's other peculiarities you'll come across. Tar you can pass a Python list of files directly using the bracket notation and it will handle that.

The solutions are to either do more on the Python side or more in the shell side. Python has it's own glob module, see here. You can combine that with working with os.system(). Python has fnamtch that is nice because you can use Unix-like file name matching. Plus there's shutil that allows moving/renaming, see shutil.move(). In Python os.remove(temp_file_name) can delete files. If you aren't working on a Windows machine there's the sh module that makes things nice. See here and here.

Wayne
  • 6,607
  • 8
  • 36
  • 93