-1

I'm working on loading a directory containing a bunch of images that I'm going to process. I am using the Azure Notebooks, and I copied the directory correctly by right-clicking + choosing 'Properties', so I don't think that it's an issue to do with how I selected my path. This is what I have tried so far:

import os
data_dir = 'C:\Users\User Name\Downloads\cell_image'

This is a direct copy of the path, but it returns the following Syntax Error: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

I then tried converting the string by putting r in front of it (and by manually adding escape \ characters to the backslashes in the string), and both returned the error shown below.

import os
data_dir = r'C:\Users\User Name\Downloads\cell_image'
os.listdir(data_dir)

This returns the following error: No such file or directory: 'C:\Users\User Name\Downloads\cell_image'

Any thoughts on how to resolve this? It's preventing me from loading my data, so any help that you can give would be amazing. Thanks!

  • If you copied your path from a Windows properties window, it might contain invisible characters that mess up the path. Put `print(repr(data_dir))` in your program and see if there's anything weird. – khelwood Feb 22 '20 at 08:32
  • I just tried that — I got the path as 'C:\\Users\\User Name\\Downloads\\cell_image'. – Isaac Nikolai Fox Feb 22 '20 at 08:37
  • It doesn't exactly seem weird on first glance, it's just got escape characters. Any thoughts on what might be going wrong? – Isaac Nikolai Fox Feb 22 '20 at 08:38
  • If it's not that, then the path must just be wrong. You could try each part in turn to find the problem, so try `'C:/Users'` then `'C:/Users/User Name'` etc. until you find the part that doesn't work. (I presume you replace `User Name` with your actual username.) – khelwood Feb 22 '20 at 08:42
  • By the way, it's easier to use forward slashes than backslashes. – khelwood Feb 22 '20 at 08:42
  • @khelwood, it says there's no such file or directory even when I just use C:/Users, so I think something is going weird earlier in the process. – Isaac Nikolai Fox Feb 22 '20 at 08:48
  • Does this answer your question? [Why do I get a SyntaxError for a Unicode escape in my file path?](https://stackoverflow.com/questions/18084554/why-do-i-get-a-syntaxerror-for-a-unicode-escape-in-my-file-path) – DARK_C0D3R Feb 22 '20 at 08:51
  • @TheVishal that was the one I started off with. I tried all of those solutions before making the post, and none of them have seemed to solve it. – Isaac Nikolai Fox Feb 22 '20 at 08:57
  • Try opening windows explorer and pasting"C:\Users\User Name\Downloads\cell_image" into it. Does it open a directory? What about command prompt: `cd C:\Users\User Name\Downloads\cell_image`? – zvone Feb 22 '20 at 09:03
  • What do you get from `print(os.path.expanduser('~'))`? – khelwood Feb 22 '20 at 09:04

2 Answers2

0

Firstly, have you replaced user name with the name of your account. (Sorry, this may sound very stupid to some but I just wanted to check.)

Next look at khelwoods comment. I would suggest you typing out the path instead of copying and pasting.

Then try changing it into variable = os.listdir("path") This will list the content of the directory and assign that string to your variable.

Otherwise I would try this in the python shell and doing it without assigning it to a variable, instead justing printing it.

Good luck!

James Ashwood
  • 469
  • 6
  • 13
  • Yeah — the User Name part is just a stand-in for my actual username, because I'm on a work computer. I'm really not quite sure why it can't see my directory. – Isaac Nikolai Fox Feb 22 '20 at 08:47
0

"/" is the python escape character, youd need to use "//"

comodo
  • 1
  • 1