0

I develope a script who get a path, read the directory who contain pics and list and store in csv file from most old to most new. But the script only work in the same folder where place the pics but not out of the folder. Traceback say: OSERROR [2] no such directory found: 'picname.jpg' And it's related with getntime. It runs in Ubuntu 16.04 and Python 2.7, for aplication Anki2.0 It is the code;

https://github.com/Marcogb81/create-CSV-images-for-anki/blob/master/image_csv_anki.py

1 Answers1

0

You must give the absolute path of the picture.

You must change:

directory.sort(key=lambda x: os.path.getmtime(x)) 

to

directory.sort(key=lambda x: os.path.getmtime(os.path.join(path, x))) 
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Hello,I follow your instructions , just I add a little change and it's works: **directory.sort(key=lambda x: os.path.getmtime(os.path.join(pathf, x)))** – Marco Garcia Baturan Apr 07 '17 at 15:39
  • And now I have this unicode error: ** line 66, in btnImport_clicked images = ["".format(pathf, elem) for elem in directory] # give format html for flashcard UnicodeEncodeError: 'ascii' codec can't encode character u'\xf1' in position 2: ordinal not in range(128)** – Marco Garcia Baturan Apr 07 '17 at 15:41
  • That error is generated because the names must have tildes or similar symbols. – eyllanesc Apr 07 '17 at 15:43
  • Add `# -*- coding: utf-8 -*-` at the top of your file. – eyllanesc Apr 07 '17 at 15:45
  • After think little bit I found the problem, when I import the photo ,python can not read the title of photo with letter 'ñ'. Y need extend this line with capacity to read ascii code beyond 128 bit. – Marco Garcia Baturan Apr 07 '17 at 15:52
  • In my code I ever put this line. But it is a problem of coding the input in archive, therefore I need extend this line with encode like this: http://stackoverflow.com/questions/9942594/unicodeencodeerror-ascii-codec-cant-encode-character-u-xa0-in-position-20 – Marco Garcia Baturan Apr 07 '17 at 15:57
  • Test with the following command: `images = ["".format(path, elem.encode('utf8')) for elem in directory] # give format html for flashcard` – eyllanesc Apr 07 '17 at 16:01
  • FInally this solution not work. I will find a solution. – Marco Garcia Baturan Apr 07 '17 at 16:02
  • My answer will help you in the initial question, please mark it as correct – eyllanesc Apr 07 '17 at 16:04
  • SOrry, I new and noob in Stackoverflow. I apologiced, I give you the solution to problem like good. You are right! – Marco Garcia Baturan Apr 07 '17 at 16:07
  • Try to be concrete in your question, if you have another question create another post, but first you have a search and verify that there is no similar question. – eyllanesc Apr 07 '17 at 16:13
  • Finally I solve the line 66 by this: **images = ["".format(pathf.encode('utf-8'), elem.encode('utf-8')) for elem in directory]** – Marco Garcia Baturan Apr 07 '17 at 16:13
  • It is a response very similar to mine, I thought the directory did not have those problems. – eyllanesc Apr 07 '17 at 16:15
  • Also you confuse me because github uses the variable `path` and you use `pathf`. – eyllanesc Apr 07 '17 at 16:16