[Tutor] python: can't open file 'ex1.py' : [Errno 2] No such file or directory
Hi everyone, This is my first email to group - I'm just starting to pick up Python and I'm going through the exercises in Zed Shaw's "Learn Python the Hard Way" ebook. Anyhow, I can't seem to be executing any files in terminal for some reason, in this case the file ex1.py: C:\Users\Carter Danforth\python ex1.py python: can't open file 'ex1.py': [Errno 2] No such file or directory ex1.py is located in "pythonpractice" on my desktop and I've updated the modules, here's the output from sys.path: ['', 'C:\\Windows\\system32\\python27.zip', 'C:\\Users\\Carter Danforth\\Desktop\\pythonpractice', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 'C:\\Python27\\lib\\site-packages'] And the environmental variable is set to have python on my path: C:\Users\Carter Danforth>python Python 2.7 (r27:82525, Jul 4 2010, 07:43:08) [MSC v.1500 64 bit (AMD64)] on win 32 Type "help", "copyright", "credits" or "license" for more information. >>> I'm not sure why I keep getting this error message and why I'm not able to execute any .py files. Any help would be great appreciated. Carter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python: can't open file 'ex1.py' : [Errno 2] No such file or directory
in a gui > Message-ID: > Content-Type: text/plain; format=flowed; charset="iso-8859-1"; >reply-type=original > > > "Karim" wrote > > > Is there any equivalent to JAVACC in python (or lex yacc) to create > > grammary > > for config or format file? > > Thats kind of what ConfiogParser does - it gives you tools to > read/write > a config file. > > If you don't mind the data not being human readable you could also > use the shelve module which simulates a dictionary in a file. > > There are lots of options. > > Alan G. > > > > > -- > > Message: 4 > Date: Wed, 25 Aug 2010 08:30:59 +0100 > From: "Alan Gauld" > To: tutor@python.org > Subject: Re: [Tutor] python: can't open file 'ex1.py' : [Errno 2] No >suchfileor directory > Message-ID: > Content-Type: text/plain; format=flowed; charset="iso-8859-1"; >reply-type=original > > > "Carter Danforth" wrote > > > Anyhow, I can't seem to be executing any files in terminal for some > > reason, in this case the file ex1.py: > > > > C:\Users\Carter Danforth\python ex1.py > > python: can't open file 'ex1.py': [Errno 2] No such file or > > directory > > > > ex1.py is located in "pythonpractice" on my desktop and I've updated > > the > > modules, here's the output from sys.path: > > sys.path (and PYTHONPATH) only affect how imports work within > Python, they have no effect on Windows ability to find scripts. > To run a script you must do one of the following: > > 1) CD to the script folder and run Python from there as: > > Python myscript.py > > 2) From anywhere execute Python as >> python full\path\to\myscript.py > > 3) From anywhere execute myscript as > > myscript.py > > For 1,2 PATH must include the Python executable folder > For 3 the .py extension must be associated with the Python > executable and the folder containing myscript must be in PATH. > > > I'm not sure why I keep getting this error message and why I'm not > > able to > > execute any .py files. Any help would be great appreciated. > > Windows needs to know where the file lives. You could have many > files called myscript.py in your file system. PYTHONPATH is used > only by Python and only for imports. PATH is used only for > executables. > > HTH, > > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > > > > -- > > Message: 5 > Date: Wed, 25 Aug 2010 01:28:47 -0700 (PDT) > From: Albert-Jan Roskam > To: Python Mailing List > Subject: [Tutor] os.access unreliable? > Message-ID: <502129.63760...@web110716.mail.gq1.yahoo.com> > Content-Type: text/plain; charset="iso-8859-1" > > Hi, > > Hi I'm using os.access to do a preliminary check to see if I have RW > access, but > it seems to be unreliable. In a dir for which I have only read access, > os.access > also says I have write access. This is under Windows 2000. I could of > course use > a try-except and catch the IOError, but I'd like to know why the code below > isn;t working. > > ??? def isAcccessible(self): > ??? if os.access(self.path, os.R_OK) and os.access(self.path, os.W_OK): > ??? return True > ??? else: > ??? return False > ?Cheers!! > Albert-Jan > > > ~~ > All right, but apart from the sanitation, the medicine, education, wine, > public > order, irrigation, roads, a fresh water system, and public health, what > have the > Romans ever done for us? > ~~ > > > > -- next part -- > An HTML attachment was scrubbed... > URL: < > http://mail.python.org/pipermail/tutor/attachments/20100825/3a660844/attachment-0001.html > > > > -- > > Message: 6 > Date: Wed, 25 Aug 2010 09:53:08 +0100 > From: Tim Golden > Cc: Python Mailing List > Subject: Re: [Tutor] os.access unreliable? > Message-ID: <4c74d9f4.3030...@timgolden.me.uk> > Content-Type: text/plain; charset=ISO-8859-1; format=flowed > > On 25/08/2010 09:28, Albert-Jan Roskam wrote: > > Hi, > > > > Hi I'm using os.access to do a preliminary check to see if I have RW > access, but > > it seems to be unreliable. In a dir for which I have only read access, > os.access > > also says I have write
[Tutor] how to create a persistent dictionary w/ cpickle?
Hi, I'm trying to a create a basic addressbook for practice. I'm using a dictionary with cpickle, though I'm not sure how to persistently store each instance in the dictionary. Below is the code I have so far. If I run it once and add a contact and the details, it's fine. p.load(f) shows the details next time I run it, but if I add another contact, and run it again, the previous key:value doesn't show. It gets replaced by the new one. How can I fix this so that it adds the new key:value to the dictionary instead of replacing the existing one? Appreciate any help, thanks. import cPickle as p addressbook = 'addressbook.data' f = file(addressbook, 'r+') class address: def __init__(self, name, tel, email): self.name = name self.tel = tel self.email = email ab = {self.name : self.tel} f = file(addressbook, 'r+') p.dump(ab, f) print p.load(f) x = raw_input() if x == 'add': name = raw_input('\nName: ') tel = raw_input('Tel: ') email = raw_input('Email: ') contact = address(name, tel, email) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to create a persistent dictionary w/ cpickle?
Dave, Steve, and Alan: late reply here, but thanks a lot guys - really appreciate the feedback. I had no idea what I was doing w/ that class in the addressbook, needed to read up more, and I got the dictionary figured out w/ cpickle and now it's all working. Thanks again, this input's really helping my learning curve. On Thu, Sep 9, 2010 at 2:19 AM, wrote: > Send Tutor mailing list submissions to >tutor@python.org > > To subscribe or unsubscribe via the World Wide Web, visit >http://mail.python.org/mailman/listinfo/tutor > or, via email, send a message with subject or body 'help' to >tutor-requ...@python.org > > You can reach the person managing the list at >tutor-ow...@python.org > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of Tutor digest..." > > > Today's Topics: > > 1. Re: how to create a persistent dictionary w/ cpickle? > (Steven D'Aprano) > 2. Re: how to create a persistent dictionary w/ cpickle? (Dave Angel) > 3. Re: how to create a persistent dictionary w/ cpickle? (Alan Gauld) > 4. Re: slicing a string (Sandip Bhattacharya) > 5. Re: slicing a string (Evert Rol) > 6. Re: slicing a string (Sandip Bhattacharya) > > > -- > > Message: 1 > Date: Thu, 9 Sep 2010 07:50:13 +1000 > From: Steven D'Aprano > To: tutor@python.org > Subject: Re: [Tutor] how to create a persistent dictionary w/ cpickle? > Message-ID: <201009090750.14230.st...@pearwood.info> > Content-Type: text/plain; charset="utf-8" > > On Thu, 9 Sep 2010 03:43:42 am Carter Danforth wrote: > > Hi, I'm trying to a create a basic addressbook for practice. I'm > > using a dictionary with cpickle, though I'm not sure how to > > persistently store each instance in the dictionary. Below is the code > > I have so far. > > > > If I run it once and add a contact and the details, it's fine. > > p.load(f) shows the details next time I run it, but if I add another > > contact, and run it again, the previous key:value doesn't show. It > > gets replaced by the new one. > > Where do you think you are *adding* a new contact? You don't. You > *replace* the existing contact with a brand new one, every time. > > The problem has nothing to do with pickle, or storing "each instance in > the dictionary". Pickle is already storing each instance in the > dictionary. The problem is that you never *add* anything to the address > book, you *replace* it each time, so there is never more than two > instances in the dictionary (one key, one value). > > You don't have an address BOOK, you only have a single address. > > > > How can I fix this so that it adds the new key:value to the > > dictionary instead of replacing the existing one? Appreciate any > > help, thanks. > > I would dump the entire address class for now and just go for something > nice and minimal. Get that working first, and then, *if necessary*, > wrap it in a class. This is Python, not Java -- we use whatever works, > and don't force everything to be a class when it doesn't have to be. > > What's the simplest address record you might have? How about a name > linked to a telephone number and email? > > address_book = {name: (tel, email), another_name: (tel, email), ...} > > So, here's the basic algorithm: > > (1) Look for the address-book. If it doesn't exist, create an empty > dictionary, and pickle it as the address-book. > > (2) Read the address-book from the pickle file. It will be a dictionary, > possibly empty. > > (3) Add an address to the dictionary. Don't create a new dictionary: > > >>> addresses = {} # creates a new, empty address book > >>> addresses["Fred"] = ("1234 5678", "f...@example.com") > >>> addresses["Betty"] = ("2468 1357", "be...@nowhere.com") > >>> addresses # not empty any more > {'Betty': ('2468 1357', 'be...@nowhere.com'), 'Fred': ('1234 > 5678', 'f...@example.com')} > > (3) Save the dictionary to the pickle file. > > > Once you have those steps happening manually, then wrap it into a class > so they happen automatically. > > > Some more comments on your code: > > > > import cPickle as p > > Now that's just lazy. While laziness in a programmer in a good thing, > this is taking it to extremes!!! You use pickle twice, three times if > you count the import. Is it really s
[Tutor] generating independent random numbers
Hi, I'm writing a program that's testing speed calculation of calendar dates from any date spanning 1600-3000. I want it to generate a random date and then prompt the user to indicate the correct day of the week using Zeller's formula. Included below is just some of the code to show what I'm having trouble with. The variables that need to be randomly called each time (in this case, 5 times) are c, y, month, and k (I use these inputs to get the value and assign the date with Zeller's formula, not included below). Problem I'm having is that on each while loop, those variables stay constant. I'm not sure how to make the class independently random. How can I change this with random so that on each while loop c, y, month, and k also have different values? Thanks --- import random, calendar class Date: c = random.randint(16,30) y = random.randint(0,99) month = random.randint(1,12) apr = [4,6,9,11] feb = [2] if month in feb: if y%4 == 0: k = random.randint(1,29) else: k = random.randint(1,28) elif month in apr: k = random.randint(1,30) else: k = random.randint(1,31) n = 0 while n < 5: Date() year = Date.c*100 + Date.y print '\n',calendar.month_name[Date.month], Date.k,',', year,'=', answer = raw_input() n+=1 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] generating independent random numbers
Thanks for the replies, Dave and Joel. The reason I'm not just using the time or datetime modules for a random date is because it's restricted to 1970-2038; I'm pulling dates from 1600-3099. Thanks a lot for the pointer about the leap years, Dave, as well the class instances; just updated it and it's all working now, and also included the rest of the code too w/ answer verification and time tracking. I want to start using this program to test myself for speed calculation using Zeller's formula, it's pretty cool for determining the days of dates - http://mathforum.org/dr/math/faq/faq.calendar.html Because of the way variables C and D are split up from the year in the formula, I split up the year for self.c and self.y. import random, time, datetime, calendar class Date: def __init__(self): self.c = random.randint(16,30) self.y = random.randint(0,99) self.month = random.randint(1,12) self.year = self.c*100 + self.y apr = [4,6,9,11] feb = [2] notleap = [1700, 1800, 1900, 3000] if self.month in feb: if self.year%4 == 0: if self.year in notleap: self.k = random.randint(1,28) else: self.k = random.randint(1,29) else: self.k = random.randint(1,28) elif self.month in apr: self.k = random.randint(1,30) else: self.k = random.randint(1,31) if self.month in [1,2]: d = self.y - 1 m = self.month + 10 else: d = self.y m = self.month - 2 z = self.k + (13*m-1)/5 + d + d/4 + self.c/4 - 2*self.c if z < 0: r = (abs(z)/7)*7 + z + 7 else: r = z%7 dict = { 0: 'Sunday', 1: 'Monday', 2: 'Tuesday', 3: 'Wednesday', 4: 'Thursday', 5: 'Friday', 6: 'Saturday' } self.day = dict[r] t1m = time.localtime().tm_min t1s = time.localtime().tm_sec t1 = t1m + t1s/100.0 n = 0 x = 0 while n < 10: newdate = Date() print '\n',calendar.month_name[newdate.month], newdate.k,',', newdate.year,'=', answer = raw_input() if answer.capitalize() == newdate.day: pass else: x += 1 n += 1 t2m = time.localtime().tm_min t2s = time.localtime().tm_sec t2 = t2m + t2s/100.0 td = t2 - t1 print '\n',x,'out of 10 wrong\nAvg time/question:',td/10,'\nTotal time:',td On Mon, Sep 27, 2010 at 10:21 PM, Dave Angel wrote: > > > On 2:59 PM, Steven D'Aprano wrote: > > On Tue, 28 Sep 2010 08:55:36 am Carter Danforth wrote: > > > class Date: > c = random.randint(16,30) > y = random.randint(0,99) > month = random.randint(1,12) > > Here's your problem: you are creating a class where all the attributes > (called "members" in some other languages) belong to the class and are > shared by all instances. > > Python classes are themselves objects, and the code inside the class > body gets executed *once*, when the class is created. So in this case, > the Date class chooses a single random month, *once*, and all instances > share this attribute Date.month. > > To get the behaviour you are after, you need to use instance attributes, > which means referring to self. The usual place to do this is in the > __init__ method, which is called when the instance is being > initialised: > > class Date: > def __init__(self): > self.month = random.randint(1,12) > # etc. > > > > By the way, why do you calculate a century and year separately, then add > c+y to get the year? It would be easier to just say: > > year = random.randint(1600, 3099) > > > > > That's the big problem, although it's also worth pointing out that you'll > need a new instance each time through the loop. It's not enough to call > Date(), you also have to bind it to a name, and use that name for attribute > lookup.So something like > mydate = Date() > year = mydate.y + > > But there are at least a few subtle problems left. One is that many of the > years are divisible by four but do not have 29 days in February. For > example, 1800, 1900, 2100 are not leap years. > > Next problem is that the dates are not evenly distributed over the entire > range of years. The 14th of February will be more likely to be chosen than > the sixth of July. You can decide that this is deliberate, but it is a > consideration. > > Third, the program doesn't do anything to check the user's answer. For > that matter, there's no timing going on either. > > Depending on the learni
Re: [Tutor] generating independent random numbers
Wow... I'm really slipping here with the leaps years, good catch on the 2000s. And yeah, a list does make a whole lot more sense. Thanks Dave. I've checked multiple sources on Zeller's formula, initially came across it on this book on vedic math (highly recommend it): http://amzn.to/bNXBM6. But here's the Wikipedia on it: http://en.wikipedia.org/wiki/Zeller%27s_congruence It's not *2 in the Julian calendar, but it is in Gregorian, which is what we're also using for the leap yrs - http://en.wikipedia.org/wiki/Leap_year On Tue, Sep 28, 2010 at 9:34 PM, Dave Angel wrote: > On 9/28/2010 5:11 PM, Carter Danforth wrote: > >> Thanks for the replies, Dave and Joel. The reason I'm not just using the >> time or datetime modules for a random date is because it's restricted to >> 1970-2038; I'm pulling dates from 1600-3099. Thanks a lot for the pointer >> about the leap years, Dave, as well the class instances; just updated it >> and >> it's all working now, and also included the rest of the code too w/ answer >> verification and time tracking. >> >> I want to start using this program to test myself for speed calculation >> using Zeller's formula, it's pretty cool for determining the days of dates >> - >> http://mathforum.org/dr/math/faq/faq.calendar.html >> >> Because of the way variables C and D are split up from the year in the >> formula, I split up the year for self.c and self.y. >> >> >> >> import random, time, datetime, calendar >> >> class Date: >> def __init__(self): >> self.c = random.randint(16,30) >> self.y = random.randint(0,99) >> self.month = random.randint(1,12) >> self.year = self.c*100 + self.y >> >> apr = [4,6,9,11] >> feb = [2] >> notleap = [1700, 1800, 1900, 3000] >> >> if self.month in feb: >> if self.year%4 == 0: >> if self.year in notleap: >> self.k = random.randint(1,28) >> else: >> self.k = random.randint(1,29) >> else: >> self.k = random.randint(1,28) >> elif self.month in apr: >> self.k = random.randint(1,30) >> else: >> self.k = random.randint(1,31) >> >> if self.month in [1,2]: >> d = self.y - 1 >> m = self.month + 10 >> else: >> d = self.y >> m = self.month - 2 >> >> z = self.k + (13*m-1)/5 + d + d/4 + self.c/4 - 2*self.c >> >> if z< 0: >> r = (abs(z)/7)*7 + z + 7 >> else: >> r = z%7 >> >> dict = { 0: 'Sunday', 1: 'Monday', 2: 'Tuesday', 3: 'Wednesday', >> 4: >> 'Thursday', 5: 'Friday', 6: 'Saturday' } >> self.day = dict[r] >> >> t1m = time.localtime().tm_min >> t1s = time.localtime().tm_sec >> t1 = t1m + t1s/100.0 >> n = 0 >> x = 0 >> >> while n< 10: >> newdate = Date() >> >> print '\n',calendar.month_name[newdate.month], newdate.k,',', >> newdate.year,'=', >> answer = raw_input() >> if answer.capitalize() == newdate.day: >> pass >> else: >> x += 1 >> n += 1 >> >> t2m = time.localtime().tm_min >> t2s = time.localtime().tm_sec >> t2 = t2m + t2s/100.0 >> td = t2 - t1 >> >> print '\n',x,'out of 10 wrong\nAvg time/question:',td/10,'\nTotal >> time:',td >> >> >> >> >> (You top-posted your response, so your message is out of order) > > I haven't tried to run your code, but there is at least one problem. > > Your notleap list is very incomplete. > > notleap = [1700, 1800, 1900, 2100, 2200, 2300, 2500, 2600, 2700, 2900, > 3000] > > I'm a little suspicious of your version of Zeller. I wouldn't think that > last term should have a 2* in it. > > I'm not sure why you use a dictionary to calculate self.day. A list would > work just as well. > > DaveA > > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] generating independent random numbers
On Wed, Sep 29, 2010 at 1:53 PM, Dave Angel wrote: > On 9/28/2010 5:11 PM, Carter Danforth wrote: > >> Thanks for the replies, Dave and Joel. The reason I'm not just using the >> time or datetime modules for a random date is because it's restricted to >> 1970-2038; I'm pulling dates from 1600-3099. Thanks a lot for the pointer >> about the leap years, Dave, as well the class instances; just updated it >> and >> it's all working now, and also included the rest of the code too w/ answer >> verification and time tracking. >> >> I want to start using this program to test myself for speed calculation >> using Zeller's formula, it's pretty cool for determining the days of dates >> - >> http://mathforum.org/dr/math/faq/faq.calendar.html >> >> Because of the way variables C and D are split up from the year in the >> formula, I split up the year for self.c and self.y. >> >> >> >> import random, time, datetime, calendar >> >> class Date: >> def __init__(self): >> self.c = random.randint(16,30) >> self.y = random.randint(0,99) >> self.month = random.randint(1,12) >> self.year = self.c*100 + self.y >> >> apr = [4,6,9,11] >> feb = [2] >> notleap = [1700, 1800, 1900, 3000] >> >> if self.month in feb: >> if self.year%4 == 0: >> if self.year in notleap: >> self.k = random.randint(1,28) >> else: >> self.k = random.randint(1,29) >> else: >> self.k = random.randint(1,28) >> elif self.month in apr: >> self.k = random.randint(1,30) >> else: >> self.k = random.randint(1,31) >> >> if self.month in [1,2]: >> d = self.y - 1 >> m = self.month + 10 >> else: >> d = self.y >> m = self.month - 2 >> >> z = self.k + (13*m-1)/5 + d + d/4 + self.c/4 - 2*self.c >> >> if z< 0: >> r = (abs(z)/7)*7 + z + 7 >> else: >> r = z%7 >> >> dict = { 0: 'Sunday', 1: 'Monday', 2: 'Tuesday', 3: 'Wednesday', >> 4: >> 'Thursday', 5: 'Friday', 6: 'Saturday' } >> self.day = dict[r] >> >> t1m = time.localtime().tm_min >> t1s = time.localtime().tm_sec >> t1 = t1m + t1s/100.0 >> n = 0 >> x = 0 >> >> while n< 10: >> newdate = Date() >> >> print '\n',calendar.month_name[newdate.month], newdate.k,',', >> newdate.year,'=', >> answer = raw_input() >> if answer.capitalize() == newdate.day: >> pass >> else: >> x += 1 >> n += 1 >> >> t2m = time.localtime().tm_min >> t2s = time.localtime().tm_sec >> t2 = t2m + t2s/100.0 >> td = t2 - t1 >> >> print '\n',x,'out of 10 wrong\nAvg time/question:',td/10,'\nTotal >> time:',td >> >> >> >> You top-posted again. Put your comments after the part you're quoting, > not before. > > You still have a problem in the code, and I still think it's in the > 2*self.c, though I don't have time to debug it. > > Look up the day for 1/1/2099, and for 1/1/2100 and it comes out the same. > That's not correct. No adjacent years start on the same day, it's always > either one day or two. > > You have too much in one function (method), which makes it hard to debug > it. Factor it into separate functions, and then test each independently. > And using k for day and d for year make no sense to me, though perhaps it > does in some other language. > > DaveA > > Hey Dave, you probably left c and y alone when comparing the years. If the date's 1/1/2099, then self.c = 20 and self.y=99. If you try doing it again while changing those values, for 1/1/2099, the day comes out to be Thursday, and for 1/1/2100 you'll get Wednesday. Glad you pointed out the 2100 date though, there actually was a problem in it, but it's not the 2*self.c; I had to account for d = y - 1 when y = 00 (zeller subtracts months by 2, so it needs to be the previous yr for jan/feb). Below is the updated code, I put in a few comments to make it read easier. -- import random, time, datetime, calendar class Date: def __init__(self): self.c