Skip to content
Advertisement

How to move files on Drive folder from Google Collab?

I’m using this code to read paths inside a txt file, the code changes the extension of the paths from jpg to json.

JavaScript

Then I need to move the json files to another folder for which I use the following line:

JavaScript

where $o is the path of the json file inside the txt file and the next path is the destination folder

However I get this error:

JavaScript

Any idea what I’m doing wrong?

Advertisement

Answer

There’s a newline at the end of $o, so your !mv $o /content/drive/.. is broken into 2 lines / commands:

JavaScript

That’s why you see 2 separate error messages.

Try replacing o = o.replace("jpg", "json") with o = o.rstrip().replace("jpg", "json") to strip newlines.

Debugging tip: using something like print(f'path: "{o}"') makes it far easier to spot such issues; and if you are not quite sure what exactly gets sent to Bash or how vars are evaluated, test your commands with echo first: !echo mv $o /content/drive/.../malas/$etiquetas/

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement