0

My first question here. I have been working with python on jupyter notebook for a personal project. I am using a code to dynamically allow users to select a csv file on which they wish to test my code on. However, I am not sure how to extract the name of this file once I have uploaded this file. The code goes on as follows:

***import numpy as np
import pandas as pd
from pandas import Series, DataFrame
import io
from google.colab import files
from scipy import stats
uploaded = files.upload()
df = pd.read_csv(io.BytesIO(uploaded['TestData.csv']))
df.head()
.
.
.*** 

As you can see, after the upload when I try to read the file, I have to type its name manually in the code. Is there a way to automatically capture the name of the file in a variable and then I can use the same while calling the pandas read function?

  • Does this answer your question? [How do I get the current IPython / Jupyter Notebook name](https://stackoverflow.com/questions/12544056/how-do-i-get-the-current-ipython-jupyter-notebook-name) – Mehdi Zare Jul 19 '20 at 17:27
  • What is `uploaded`? Your use of `uploaded['TestData.csv']` suggests it is a `dict`, keyed by the filename. If so, `list(uploaded.keys())` gives a list of those names. – hpaulj Jul 19 '20 at 17:38
  • Thanks, @hpaulj. The first key associated with the dict was the filename which I was able to extract. And, I used uploaded for asking the users to choose the file they want to upload and work on from their directory through the command files.upload. – Anurag Mandal Jul 19 '20 at 18:44

0 Answers0