1

I have a code which perfectly works when executed in IDLE buts the same piece of code shows error when executed in google colab.

code snippet:

im_path = os.path.join('D:\ANIKET\movie data set sentiment analysis','aclImdb')
train_texts = []
train_labels = []

for category in ['pos','neg']:
    train_path = os.path.join(im_path,'train',category)
    for fname in sorted(os.listdir(train_path)):
        if fname.endswith('.txt'):
            with open(os.path.join(train_path, fname),encoding = 'utf8') as f:
                train_texts.append(f.read())
            train_labels.append(0 if category == 'neg' else 1)

colab error:

FileNotFoundError                         Traceback (most recent call last)
<ipython-input-3-9c42cfcaed98> in <module>()
     19     train_path = os.path.join(im_path,'train',category)
     20 
---> 21     for fname in sorted(os.listdir(train_path)):
     22         if fname.endswith('.txt'):
     23 

FileNotFoundError: [Errno 2] No such file or directory: 'D:\\ANIKET\\movie data set sentiment analysis/aclImdb/train/pos'
Dominique
  • 16,450
  • 15
  • 56
  • 112
Aniket Bote
  • 3,456
  • 3
  • 15
  • 33
  • As an aside, your string path literal is [unsafe](https://stackoverflow.com/questions/19065115/python-windows-path-slash), because backslash is an escape character. – Lie Ryan Dec 19 '18 at 10:02

2 Answers2

1

Unless you set up a local runtime, colab codes run on Google's server, which is likely running on a Linux environment, and it doesn't have access to your local files.

You either need to upload these files to the server first (and adjust the file path) or you'd need to set up local runtime.

Lie Ryan
  • 62,238
  • 13
  • 100
  • 144
  • How should I set local runtime? I want to access the files that are on my local machine. – Aniket Bote Dec 19 '18 at 10:02
  • 1
    It's going to be a lot easier to just upload your files, but if you really want to do it, read the [manual](https://research.google.com/colaboratory/local-runtimes.html). – Lie Ryan Dec 19 '18 at 10:05
  • You mean upload my folder on drive then import drive in colab and use path? – Aniket Bote Dec 19 '18 at 10:07
  • That's one way to do it, Drive will be useful for two ways sync, though for one off data file, it may be easier to use the File upload tab. [Further info](https://colab.research.google.com/notebooks/io.ipynb). – Lie Ryan Dec 19 '18 at 10:15
0

You could try to change:

im_path = os.path.join('D:\ANIKET\movie data set sentiment analysis','aclImdb')

into

im_path = os.path.join('D:/ANIKET/movie data set sentiment analysis','aclImdb')

To enforce an uniform path?

romanoww
  • 121
  • 1
  • 1
  • 6
  • Didn't work. Still showing the same error but now the error contains backslash instead of forward slash. – Aniket Bote Dec 19 '18 at 09:57
  • Lie Ryan's comment is correct. Just connect to local and you're all set. https://research.google.com/colaboratory/local-runtimes.html – romanoww Dec 19 '18 at 10:03
  • 1
    If you have anaconda installed, it should be really easy to enable jupyter and run locally (few lines of code in Anaconda Prompt). Afterwards, make sure you change it in de Colab GUI (top right of screen). – romanoww Dec 19 '18 at 10:11