Reputation: 168
I understand that this question may exist, however I am just trying to figure out my formatting. I am not the best when it comes to formatting things like this and am just doing a bit of guess work that just leads to issues.
I have a custom file type that it must save to, a .gcom
and a file name that is declared with the function. Currently the function looks like this:
def cComFile(): # This will NOT write command code (Yet) I want to see if file creation works!
filetype='.gcom'
commands_Directory = 'C:\\Genisis_AI\\Commands\\' # Unused at the moment
file_name = command
if os.path.isfile(("C:\\Genisis_AI\\Commands\\{}").format(file_name).join("{}").format(filetype)):
a = raw_input(('{} already exists! Would you like to overwrite this? [Y/N]: ').format(cComFile.file_name))
string.lower(a)
if a == 'y':
print(("Command: {} , has been rewritten!").format(file_name))
elif a == 'n':
pass
else:
f = open((cComFile.commands_Directory.join(cComFile.file_name.join(cComFile.filetype)),'w+'))
f.write('command was made')
print(('Command made" {}').format(cComFile.command))
I am getting an issue with line 135 which is the following line:
if os.path.isfile(("C:\\Genisis_AI\\Commands\\{}").format(file_name).join("{}").format(filetype)):
The error I am receiving is the following:
Traceback (most recent call last):
File "testing/aaa.py", line 149, in <module>
cComFile('test')
File "testing/aaa.py", line 135, in cComFile
if os.path.isfile(("C:\\Genisis_AI\\Commands\\{}").format(file_name).join("{}").format(filetype)):
KeyError: 'C'
I understand that I am most likely doing this all the wrong way but its a learning experience I suppose. And the chances are that me getting that wrong means that the formatting for the rest of this function are also incorrect as well. Thank you for reading and any help would be nice.
(I understand this is messy and I don't mind it at the moment, I tend to clean up after things work)
Upvotes: 1
Views: 940
Reputation: 408
You can pass several arguments to format
:
if os.path.isfile("C:\\Genisis_AI\\Commands\\{}.{}".format(file_name, filetype)):
Or if you're using Python 3.6 (note the f
before the string):
if os.path.isfile(f"C:\\Genisis_AI\\Commands\\{file_name}.{filetype}"):
Or if you prefer the %
syntax:
if os.path.isfile("C:\\Genisis_AI\\Commands\\%s.%s" % (file_name, filetype)):
Upvotes: 2
Reputation: 525
So basically you jut want to check if a file exists? Why not use a try statement instead of the if?
import errno
try:
with open(filename,'w') as file:
# do stuff with the file
except IOError as e:
# if returned error is [Errno 2] "No such file or directory"
if e.errno == errno.ENOENT:
#do something else
This uses the errno module which makes available error codes from your system. This works for me on windows, may be slightly different on Linux not sure.
Upvotes: 0
Reputation: 1217
you can pass multiuple values to format function like this, with multiple '{}'
.
"C:\\Genisis_AI\\Commands\\{}.{}".format(file_name, filetype)
Upvotes: 1