My program cannot find the path that it just made, the program is for sorting files in the download folder. If it finds a new type of file it should make a folder for that file type.
import os
FileList = os.listdir("/sdcard/Download/")
for File in FileList:
#print File
extension = ''.join(os.path.splitext(File)[1])
ext = extension.strip('.')
if os.path.exists("/mnt/external_sd/Download/" + ext):
Data = open("/sdcard/Download/" + File, "r").read()
file("/mnt/external_sd/" + ext + "/" + File, "w").write(Data)
elif os.path.exists("/mnt/external_sd/Download/" + ext) != True:
os.makedirs("/mnt/external_sd/Download/" + ext)
Data = open("/sdcard/Download/" + File, "r").read()
file("/mnt/external_sd/" + ext + "/" + File, "w").write(Data)
1 Answer 1
You create the directory
"/mnt/external_sd/Download/" + ext
but then you are trying to write to
"/mnt/external_sd/" + ext + "/" + File
You dropped the Download folder in that path. Change the last line to:
file("/mnt/external_sd/Download/" + ext + "/" + File, "w").write(Data)
Incidentally, it would be a bit shorter and clearer to write your last seven lines by taking the shared lines out of the if else statement and using shutil.copy instead of reading in the whole file then writing it out again:
import shutil
if not os.path.exists("/mnt/external_sd/Download/" + ext):
os.makedirs("/mnt/external_sd/Download/" + ext)
shutil.copy("/sdcard/Download/" + File, "/mnt/external_sd/Download/" + ext + "/" + File)
(Using shutil will also generally be faster and use less memory, especially if your files are large).
os.makedirs("/mnt/external_sd/Download/" + ext)create a directory, and not a file, which you then try to open?"/" + Fileto it.