views:

141

answers:

5

Hi all...First python script and I'm getting an error I can't seem to get around using a config file. The first part of the script takes user input and puts that into a mysql database with no problem..Then I get to the filesystem work and things go a bit pear shaped..I can get it to work without using the config file options but I'd like to keep it consistent and pull from that file:

  vshare = str(raw_input('Share the user needs access to: '))
  vrights = str(raw_input('Should this user be Read Only? (y/n): '))
  f = open("%s/%s" % (config['vsftp']['user_dir'], (vusername), 'wr'))
  #f = open("/etc/vsftpd_user_conf/%s" % (vusername) , 'wr' )
  f.write("local_root=%s/%s" % (config['vsftp']['local_root_dir'], vshare))
  if vrights.lower() in ['y', 'ye', 'yes']:
      buffer = []
      for line in f.readlines():
          if 'write_enable=' in line:
              buffer.append('write_enable=NO')
          else:
              buffer.append(line)
      f.writelines(buffer)
  f.close()

The error I'm getting is:

TypeError: not all arguments converted during string formatting

If I uncomment the commented line it works and makes it a bit further and errors out as well..But I'll deal with that once I get this hiccup sorted..Thanks for any input.

+2  A: 

The error is here:

open("%s/%s" % (config['vsftp']['user_dir'], (vusername), 'wr'))

You have three parameters, but only two %s in the string. You probably meant to say:

open("%s/%s" % (config['vsftp']['user_dir'], vusername), 'wr')

Although 'wr' is unclear, you probably mean w+ or r+.

http://docs.python.org/library/functions.html#open

Lennart Regebro
+2  A: 

Your tuple is misshaped

f = open("%s/%s" % (config['vsftp']['user_dir'], (vusername), 'wr'))

Should be

f = open("%s/%s" % (config['vsftp']['user_dir'], (vusername)), 'wr')
cdleary
That did it...Thanks to all that replied..That was fast!! Ran into the second error which is a bad file descriptor error..Trying to get that one sorted out on my own..Thanks again!
Cube_Zombie
A: 
f = open("%s/%s" % (config['vsftp']['user_dir'], (vusername), 'wr'))

You are passing three arguments (config['vsftp']['user_dir'], (vusername), 'wr') to a format string expecting two: "%s/%s". So the error is telling you that there is an argument to the format string that is not being used.

Nick Lewis
A: 

I think you have a wrong parenthesis, your line should be:

f = open("%s/%s" % (config['vsftp']['user_dir'], (vusername)), 'wr')
Marco Mustapic
A: 

It looks like this line should be:

f = open("%s/%s" % (config['vsftp']['user_dir'], vusername), 'wr')

(I moved the closing parenthesis over.)

steamer25