0

My script uses os.listdir to get a list of directories to use later for batch analysis.

when running

mypath='//home//user//Documents//data'
datalist=os.listdir(mypath)

in console, I get the correct answer.

However, when I use the same code as a part of the script, python falls over on the datalist line

FileNotFoundError: [Errno 2] No such file or directory: '//home//user//Documents//data//'
lpriba
  • 9
  • 2
  • Your code does not fit the error message. One has slashes the other has backslashes. – Klaus D. Oct 02 '17 at 11:19
  • Are you sure that this path exists? Also, if you use forward slash(/), you shuoldn't double it. Just use `mypath = '/home/user/Documents/data'` To prevent confusion, use "r" such as `mypath = r'/home/user/Documents/data'` or `mypath = r'\home\user\Documents\data'` – Alperen Oct 02 '17 at 11:21
  • edited now, apologies – lpriba Oct 02 '17 at 11:22
  • There is a typo in "documents" being returned by your error message. Have you spelt the path correctly? – QHarr Oct 02 '17 at 11:25
  • usint mypath=r'\home\user\Documents\data' worked! what does that do? – lpriba Oct 02 '17 at 11:27
  • Are you using Cygwin or MSYS2 by chance? What are your platform details? – Josh Lee Oct 02 '17 at 11:37

1 Answers1

0

Quoted from here:

The r'..' string modifier causes the '..' string to be interpreted literally. That means, r'My\Path\Without\Escaping' will evaluate to 'My\Path\Without\Escaping' - without causing the backslash to escape characters. The prior is equivalent to 'My\\Path\\Without\\Escaping' string, but without the raw modifier.

And there is an explanation with a simple example at this link.

So use

mypath = r'\home\user\Documents\data'
Alperen
  • 3,772
  • 3
  • 27
  • 49