I'd like to apply the command repeatedly to files that exist sequentially in Python/folder. Help me.

Asked 1 years ago, Updated 1 years ago, 122 views

Hello, I'm Python's beginner. I've been doing this and that by myself, but I don't know the answer, so I'm leaving a question.

The code is as follows.

import glob

list_filepath = glob.glob('/content/drive/MyDrive/Final Sheets_python/raw/901000_Sheet/*.ipynb')

for filepath in list_filepath:
    print(filepath)

%cd /content/drive/MyDrive/Final Sheets_python/raw/901000_Sheet/

run 901000-1.ipynb

%cd /content/drive/MyDrive/Final Sheets_python/raw/901000_Sheet/

run 901000-2.ipynb

There are five files in the folder, 901000-1, 2, 3, 4, 5. Until now, every time I turned 5 files, I kept using the %cd command to move the path and run, but I thought it was a waste of time.

Is there a way to create a code that can repeat %cd and run in the order of the number of files in the folder? I searched with keywords such as path movement and for statement, but I don't know what information to use.

I ask for your advice.

Thank you for reading.

python loops

2022-09-22 22:56

1 Answers

import subprocess

...

You can enter commands by using the subprocess library!

If you have any more questions, please let me know. :)


2022-09-22 22:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.