3 Replies - 185 Views - Last Post: 06 November 2013 - 01:08 PM Rate Topic: -----

#1 DeviantYoda  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 08-July 13

New to file stuff, would like some advice.

Posted 29 October 2013 - 07:47 PM

As said in title, I'm new to managing .txt files and want some help.

To start, I created a pretty basic account creator, which had some functions to edit the data, create an account, etc.
Basically, I just want to know if there is any glaringly inefficient code, and how to fix it.

import os
import sys
import shutil

def Create():
	username = raw_input('Enter a username: ')
	if username == 'Incorrect' or username == 'All':
		print 'Sorry, that username is unavailable.'
	else:
		if os.path.isdir('/Users/rohr8293/Service/Logins/' + username) == False:
			password = raw_input('Enter a password: ')
			os.mkdir('/Users/user/Service/Logins/' + username)
			os.mkdir('/Users/user/Service/Logins/' + username + '/data')
			new_login = open('/Users/user/Service/Logins/' + username + '/login.txt', 'w')
			new_login.write(password)
			new_login.close()
			os.rename('/Users/user/Service/Logins/' + username + '/login.txt', '/Users/user/Service/Logins/' + username + '/login.service')
		else:
			print 'Sorry, an account with that username already exists.'

def Login():
	username = raw_input('Enter your username: ')
	if os.path.isdir('/Users/user/Service/Logins/' + username) == True:
		os.rename('/Users/user/Service/Logins/' + username + '/login.service', '/Users/user/Service/Logins/' + username + '/login.txt')
		login = open('/Users/user/Service/Logins/' + username + '/login.txt', 'r')
		pw = login.readline()
		os.rename('/Users/user/Service/Logins/' + username + '/login.txt', '/Users/user/Service/Logins/' + username + '/login.service')
		password = raw_input('Enter your password: ')
		if pw == password:
			sys.stdout.write(username)
		else:
			sys.stdout.write('Incorrect')
		
def Remove():
	username = raw_input('Enter your username: ')
	if username == 'All':
		password = raw_input('Please enter the override password: ')
		if password == 'sudo': #hehe
			shutil.rmtree('/Users/user/Service/Logins')
			os.mkdir('/Users/user/Service/Logins')
			print 'All accounts removed.'
		else:
			print 'Incorrect override password.'
	else:
		if os.path.isdir('/Users/user/Service/Logins/' + username) == True:
			os.rename('/Users/user/Service/Logins/' + username + '/login.service', '/Users/user/Service/Logins/' + username + '/login.txt')
			login = open('/Users/rohr8293/Service/Logins/' + username + '/login.txt', 'r')
			pw = login.readline()
			login.close()
			password = raw_input('Enter your password: ')
			if password == pw:
				shutil.rmtree('/Users/rohr8293/Service/Logins/' + username)
				print 'Account removed.'
			else:
				print 'Sorry, that password is incorrect.'
				os.rename('/Users/rohr8293/Service/Logins/' + username + '/login.txt', '/Users/rohr8293/Service/Logins/' + username + '/login.service')
		else:
			print 'Sorry, an account with that username does not exist.'


If any of the code seems stupid considering I will be the only one using it, its because I like to pretend tons of people use my programs and design them with that mindset.

Is This A Good Question/Topic? 1
  • +

Replies To: New to file stuff, would like some advice.

#2 DeviantYoda  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 08-July 13

Re: New to file stuff, would like some advice.

Posted 29 October 2013 - 07:52 PM

Note: please ignore the fact that the user value is occasionally both rohr8293 and user, a slight want for anonymity combined with bad eye sight/editing skills came into play amazingly well.
Was This Post Helpful? 0
  • +
  • -

#3 jbonnick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 06-November 13

Re: New to file stuff, would like some advice.

Posted 06 November 2013 - 12:50 PM

A few things to keep in mind when working with .txt files.

The second parameter of the open() method (e.g. 'r' or 'r+') your options are:
'w' - Write to a file already in existence, overwrite data already there.
'a' - Append the text in a file already in existence.
'r' - Read from a file.
'w+' - This does the same as 'w' but creates the file if the system cannot find the file.
'a+' - This does the same as 'a' but creates the file if the system cannot find the file.
'r+' - This does the same as 'r' but creates the file if the system cannot find the file.

I would also suggest you turn the whole thing into a class, so that you can use data from methods. (e.g. self.username) would only have to be used once and the whole system would be a lot more organised.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2877
  • View blog
  • Posts: 9,548
  • Joined: 12-December 12

Re: New to file stuff, would like some advice.

Posted 06 November 2013 - 01:08 PM

Nothing to do with efficiency, but writing expressions like '/Users/user/Service/Logins/' repeatedly makes the code hard to read, and will make it cumbersome to modify later. Put some of these strings, or parts of them, into variables. If you use a Class as jbonnick suggests then these could be attributes (properties, in other languages) for this Class.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1