As the error message states, you (well, the user your script is running as) don't have permissions to create files in whatever the current working directory is.
Now before you start checking your directory permissions, you have to understand that the way it's currently written, your script will not create the file in "var/createcode" but in whatever the current working directory is at this moment - IOW if you launch your script from /home/someuser, it will try and create the file in /home/someuser.
To make sure your file is created where you expect it, you must specify an absolute path - any relative path will be "relative to the current working directory". This means you must either hardcode the absolute path (not very practical), derive it from some environment variable, or (in your case) derive it from the script's directory, using the __file__ magic variable:
import os
# the directory were the script is (ie /var/createcode/code)
here = os.path.abspath(os.path.dirname(__file__))
# you say you want you file to be created in /var/createcode, so
# you want the parent directory
parent = os.path.dirname(_here)
# now we can build the path
filepath = os.path.join(parent, "data.txt")
print("trying to create {}".format(filepath))
with open(filepath, "w") as f:
f.write("hello world")
At this point, if you still have permissions issues, you'll have to check the permissions for the user under which your process is running - at least you know your checking the permissions for the correct directory ;-)