Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 25th, 2004, 07:31 PM
XxChris XxChris is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 217 XxChris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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()

Reply With Quote
  #2  
Old January 25th, 2004, 10:36 PM
SolarBear's Avatar
SolarBear SolarBear is offline
onCsdfeu
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Canada
Posts: 100 SolarBear User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 16 m 21 sec
Reputation Power: 10
Send a message via MSN to SolarBear
What error do you get ? Your script worked when I tried it (under Win XP though).

Reply With Quote
  #3  
Old January 25th, 2004, 10:46 PM
XxChris XxChris is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 217 XxChris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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...

Reply With Quote
  #4  
Old January 26th, 2004, 06:45 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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"

Quote:
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


Reply With Quote
  #5  
Old January 26th, 2004, 09:18 AM
XxChris XxChris is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 217 XxChris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks alot

Reply With Quote
  #6  
Old January 26th, 2004, 10:03 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
No problem happy to help!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Working with files

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap