I have a file: E:\test.blend
Why does the following code not print “true”?
FWIW, I got the file_exists code from:
https://www.w3schools.com/python/python_file_remove.asp
Also, is there a simple way to figure out the filepath (Windows for now) when referencing a .blend file for import/append?
import bpy
import os
def file_exists(filepath):
if os.path.exists("filepath"):
return "true"
else:
return "false"
my_filepath = "E:\test.blend"
print (file_exists(my_filepath ))
Found (from Medium) a nice post on all this:
import bpy
from pathlib import Path
filename = Path("E:/fred.blend")
print(filename.name)
# prints "raw_data.txt"
print(filename.suffix)
# prints "txt"
print(filename.stem)
# prints "raw_data"
if not filename.exists():
print("Oops, file doesn't exist!")
else:
print("Yay, the file exists!")
This works and has the added advantage of automatically working on Mac, Linux and PC.
https://medium.com/@ageitgey/python-3-quick-tip-the-easy-way-to-deal-with-file-paths-on-windows-mac-and-linux-11a072b58d5f
if os.path.exists(“filepath”):
ehh… you really dont see the quotes around your would-be variable?