0

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"))
Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
  • 2
    does the debug `key` print print the correct thing? Are you on a Windows machine (it looks like it, as those are Windows paths). – Philip Massey Jul 02 '14 at 03:42
  • No it returns that the file is missing even if it is there – user3739525 Jul 02 '14 at 03:43
  • wait yes.. it prints what is written in the config file .. yes windows – user3739525 Jul 02 '14 at 03:44
  • And these are just regular files yes? Not links or mounts or folders? – Philip Massey Jul 02 '14 at 03:45
  • Regular text files that physically live at the location – user3739525 Jul 02 '14 at 03:46
  • Show us the output, we need to see `y` and `key`. – paxdiablo Jul 02 '14 at 03:47
  • I was thinking there was a problem with the way the path is formed for os.path.isfile – user3739525 Jul 02 '14 at 03:48
  • y output is server1 key output is "C:\\test1.txt" – user3739525 Jul 02 '14 at 03:49
  • 1
    It shouldn't make a difference, but try "C:/test1.txt" instead of "C:\\test1.txt" – Philip Massey Jul 02 '14 at 03:51
  • changing the path to c:\ did not work... however I did find something. I created a file called test1.txt in the program directory and I changed if os.path.isfile('test1.txt'): the program works.. so i decided to go with that and changed the ini file to key = "test1.txt" and it failed when i changed back to if os.path.isfile((key)): there must be something wrong with the way i am using the variable key – user3739525 Jul 02 '14 at 04:19
  • @user3739525, the reason it works when hardcoded is that the quotes _delimit_ the string, eg `xyzzy`. In the INI file, they're _part_ of the string, `"xyzzy"`. See my answer below with a couple of possibilities for fixing this. – paxdiablo Jul 02 '14 at 04:22

1 Answers1

1

If you examine the output of your program, you'll see that the key value is:

"C:\\test1.txt"

The problem is the quotes. If you get rid of those, it works okay:

server.ini:
    [server1]
    key = C:\test1.txt
    status = 3

    [server2]
    key = M:\test2.txt
    status = 1

If you do not have the power to change the file for some reason (it may be provided externally), you can strip off the quotes before using the key, changing:

key = config.get((y), 'key')

into:

key = config.get((y), 'key').strip('"')

You'll also notice that I've removed the double backslashes since they're something you need with string literals in your source code. Within the strings of the INI file, a single backslash works just as well.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Thankyou very much... I never though to try to strip the "" marks out of the ini file.. I tried everything else... Thanks that did the job – user3739525 Jul 02 '14 at 04:29