Ich habe Probleme beim Erstellen eines Verzeichnisses und dann beim Öffnen/Erstellen/Schreiben in eine Datei im angegebenen Verzeichnis. Der Grund scheint mir unklar zu sein. Ich verwende os.mkdir () und
path=chap_name
print "Path : "+chap_path #For debugging purposes
if not os.path.exists(path):
os.mkdir(path)
temp_file=open(path+'/'+img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
Ich bekomme den Fehler
path=chap_name
print "Path : "+chap_path #For debugging purposes
temp_file=open(img_alt+'.jpg','w')
temp_file.write(buff)
temp_file.close()
print " ... Done"
Immer noch einen Fehler erhalten. Weiter verwirrt.
Update 2: Das Problem scheint das img_alt zu sein, es enthält in einigen Fällen ein '/', was dazu führt, dass es den Ärger verursacht.
Also muss ich das '/'. behandeln. Gibt es sowieso das'/', oder ist das Löschen die einzige Option?
import os
path = chap_name
if not os.path.exists(path):
os.makedirs(path)
filename = img_alt + '.jpg'
with open(os.path.join(path, filename), 'wb') as temp_file:
temp_file.write(buff)
Wichtig ist, os.makedirs
anstelle von os.mkdir
zu verwenden. Es ist rekursiv, d. H. Es erzeugt alle Zwischenverzeichnisse. Siehe http://docs.python.org/library/os.html
Öffnen Sie die Datei im Binärmodus, während Sie Binärdaten (JPEG-Daten) speichern.
Als Antwort auf Edit 2, wenn img_alt manchmal '/' drin ist:
img_alt = os.path.basename(img_alt)
import os
os.mkdir('directory name') #### this command for creating directory
os.mknod('file name') #### this for creating files
os.system('touch filename') ###this is another method for creating file by using unix commands in os modules