import tempfile
import shutil
temp_ = tempfile.mkdtemp()
class ListView_(Screen):
def Image_(self, path):
global image_file_path
file_path = shutil.copy2(path[0], temp_)
in python 3 file_path output is a "path"
in python 2 file_path output is "None" so how to get the path of the new file in temp directory
shutil
comes with source, so you can look into python 3 version and adapt it.
If you compare shutil.copy2
methods for Python 2.7 & 3.4, you'll notice a new return dst
in the 3.4 version. That's a new feature added, not present in python 2.
the interesting lines for you in shutil.copy2
method are:
if os.path.isdir(dst):
dst = os.path.join(dst, os.path.basename(src))
It means that if temp_
is a directory, then target is the directory / basename of the source, else leave it as is, so after this code, dst
is always a target file name, and open(dst,"wb')
will work.
so to make your code compatible with python 2 & 3 (which is a good thing when it's possible), you can emulate that by using a ternary expression to compute the actual filepath target, then use it directly in shutil
(why passing a directory again ?):
file_path = os.path.join(_temp, os.path.basename(src)) if os.path.isdir(temp_) else temp_
shutil.copy2(path[0], file_path) # ignore return code