laserpython
laserpython

Reputation: 300

python IOError [Errno 13] string vs Tk

When I use Tk.askopenfiledialogbox and choose the directory I want, then open each file with open(files, "r") it works fine. However, when I hard code the path into a string I get access denied IOError [Errno13]. Here is my code:

   data_path = "C:\Data\DataSubDir"  
   datadir = [x[0] for x in os.walk(data_path)]
   for dataset in datadir[1:]:
        for files in glob.glob(dataset):
             with open(files,'r') as dest_f:
                  data_iter = c.reader(dest_f, 
                                       delimiter = ',', 
                                       quotechar = '"')
                   data = [data for data in data_iter]
                   csv = np.asarray(data, dtype = None)

I would like to solve this with out changing permissions. Also, if someone could also explain why choosing file through a Tk dialog box gets rid of the permission issue that would be awesome (and offer a solution too! ).

Thanks.

Upvotes: 0

Views: 40

Answers (1)

cdarke
cdarke

Reputation: 44394

Either escape your backslashes

data_path = "C:\\Data\\DataSubDir"

or use a raw string:

data_path = r"C:\Data\DataSubDir"  

With a Tk dialog box the string is in the correct format.

Upvotes: 2

Related Questions