0

I'm trying to upload and replace a file using google colab.

Currently what I do is

from google.colab import files
upload = files.upload() 

then if I need to modify the file, I do it locally on my computer. If I upload it again using the same cell, the new version of the file will be uploaded as "filename(1)". I would like the new version to replace the old one.

What I do then is

!rm "filename"

And then I run the first cell again. But it is not great.

Is there an option like what follows ?

upload = files.upload(replace=True) 
  • Does this answer your question? [How can access Uploaded File in Google colab](https://stackoverflow.com/questions/48485255/how-can-access-uploaded-file-in-google-colab) – Andrew Chisholm Aug 29 '22 at 21:23

1 Answers1

0

My approach is as follows.

lsdi = os.listdir('/content')

uploaded = files.upload()

for fn in uploaded.keys():
  print('User uploaded file "{name}" with length {length} bytes'.format(
      name = fn, length = len(uploaded[fn])))

if fn in lsdi:
    os.remove(fn)
    lsdi = os.listdir('/content') # list is in arbitrary order
    for k in sorted(lsdi,reverse=True): # sorted to get the most recent file name.
        fil_dados = re.match(fn[:fn.rfind('.')],k)
        if fil_dados:
            fn = k

  • When I entered the source code of "files.upload()" I noticed that the 'upload()' function had no argument, so I don't think there's a way to insert an option like you're looking for. ` def upload(): """Render a widget to upload local (to the browser) files to the kernel. Blocks until the files are available. Returns: A map of the form {: } for all uploaded files. """ ` – rafael de jesus silva monteiro Sep 02 '22 at 18:30