#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    217
    Rep Power
    0

    Working with files


    I'm not sure why this doesn't work. It's pretty simple... just tests if it can read/write the file.

    Code:
    #! /usr/bin/env python
    
    import sys
    
    class FileCheck:
    	def check(self, filename):
    		try:
    			fp = open("filename", "r")		
    			print "Can read from " + filename
    			fp.close()
    		except:
    			print "Cannot read from " + filename
    		
    		try:
    			fp = open("filename", "w")
    			print "Can write from " + filename
    			fp.close()
    		except:
    			print "Cannot read from " + filename
    
    def main():
    	file = FileCheck()	
    
    	if len(sys.argv) != 2:
    		print "Usage: ./filecheck <path/filename>"
    	else:
    		file.check(sys.argv[1])
    		
    if __name__ == "__main__":
    	main()
  2. #2
  3. onCsdfeu
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Canada
    Posts
    100
    Rep Power
    12
    What error do you get ? Your script worked when I tried it (under Win XP though).
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    217
    Rep Power
    0
    When I give it a filename that doesn't exist it still says it can read/write. When I type "fp =open("oiwjedoiwjed", "r") directly into the interpreter, I get an exception. But in my program the except: stuff doesn't run...
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Could have somthing to do with the filename you passing to your files. instead of using the variable 'filename' your passing in a string "filename"

    C:\Documents and Settings\Mark>cd desktop

    C:\Documents and Settings\Mark\Desktop>checkfile.py new.txt
    Can read from <open file 'new.txt', mode 'r' at 0x008F6F60>
    Can write to <open file 'new.txt', mode 'w' at 0x008F6F60>
    Code:
    #!/usr/bin/env python
    
    def check(path):
    	
    	"""
    	Check a path to see if the file is readable/writable or not.
    	"""
    	
    	try:
    		print 'Can read from', file(path, 'r')
    	except IOError:
    		print 'Cannot read from', path
    	
    	try:
    		print 'Can write to', file(path, 'w')
    	except IOError:
    		print 'Cannot write to', path
    
    if __name__ == "__main__":
    	
    	import sys	
    	
    	if len(sys.argv) != 2:
    		print 'Usage: ./filecheck <path/filename>'	
    	else:
    		check(sys.argv[1])
    changed things around and it worked fine.. oh you really dont need a class there; people seems to use classes where a function would be fine, so not just you.

    Mark.
    programming language development: www.netytan.com Hula

  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    217
    Rep Power
    0
    Thanks alot
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    No problem happy to help!
    programming language development: www.netytan.com Hula


IMN logo majestic logo spyfu logo threadwatch logo seochat tools logo