Greetings I am a python noob who is trying to put something together. Essentially I have a ini file that contains absolute paths to files and I want python to test if the file exists. I am having an error where the file always comes up as missing and I think it has to do with the way I am formatting the path. I may be completely wrong.
Please keep in mind that the loop has not been created yet I am trying to solve this one problem first. However any advice on the rest of the program would also be appreciated. My intention is to have it loop though all sections seeing if the file labelled key exists. Then it sets a value in the config file labelled status accordingly.
here is my config file:
[server1]
key = "C:\\test1.txt"
status = 3
[server2]
key = "M:\\test2.txt"
status = 1
here is my program:
#IMPORTS
import ConfigParser
import os
import os.path
#SET SECTION NUMBER
x = 1
y = 'server'+ str(x)
#DEBUG
print y
#READ CONFIG FILE
config = ConfigParser.ConfigParser()
config.readfp(open(r'server.ini'))
key = config.get((y), 'key')
#DEBUG
print (key)
#CHECK IF FILE EXISTS
if os.path.isfile((key)):
print "file is there"
config.set((y), 'status', '1')
config.write(open(r'server.ini', "w"))
else:
print "file is missing"
config.set((y), 'status', '3')
config.write(open(r'server.ini', "w"))