Re: [Tutor] how to expand "../../myfile"
On 2014-07-18 23:44, Alan Gauld wrote: On 19/07/14 02:33, Alex Kleider wrote: Just to be able to glean the current working directory would suffice. os.getcwd() solutions are tumbling from the sky! Thank you, Alan. alex ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Read, Write, Split and Append lines from a text file
I have given it all I got: a week trying to open, read, write, split, append, convert to integers and then manipulate the data. I have attached the file I want to pull the names and numbers from. I'll cut and paste it incase that doesn't work and then include a sample of the python shell code. AK 36 AL 39 AR 39 AZ 45 CA 61 CO 54 CT 61 DC 93 DE 62 FL 51 GA 47 HI 72 IA 54 ID 36 IL 62 IN 50 KS 41 KY 41 LA 40 MA 62 MD 62 ME 58 MI 57 MN 54 MO 49 MS 43 MT 47 NC 50 ND 45 NE 41 NH 54 NJ 57 NM 57 NV 55 NY 62 OH 51 OK 34 OR 57 PA 55 RI 63 SC 45 SD 45 TN 42 TX 44 UT 34 VA 53 VT 67 WA 58 WI 56 WV 43 WY 33 # and now my newbie attempt: # I can get this far in approach #1 >>> filename = "z:/Geog482/egund919/Program1/BOJ.txt" >>> myfile = open(filename,"r") >>> newfile = "z:/Geog482/egund919/Program1/BOJ_B.txt" >>> mynewfile = open(newfile,"w") >>> while True: line = myfile.readline() print line mynewfile.write(line) if not line: break States OJ AK 36 AL 39 AR 39 AZ 45 CA 61 CO 54 CT 61 DC 93 DE 62 FL 51 GA 47 HI 72 IA 54 ID 36 IL 62 IN 50 KS 41 KY 41 LA 40 MA 62 MD 62 ME 58 MI 57 MN 54 MO 49 MS 43 MT 47 NC 50 ND 45 NE 41 NH 54 NJ 57 NM 57 NV 55 NY 62 OH 51 OK 34 OR 57 PA 55 RI 63 SC 45 SD 45 TN 42 TX 44 UT 34 VA 53 VT 67 WA 58 WI 56 WV 43 WY 33 In approach number 2: trying to append, convert to integer, but the list comes up empty: >>> infile = open('C:/Python27/egund919/Program1/BOJ_B.txt','r') >>> import string >>> state_name = [] >>> data_value = [] >>> counter = 0 >>> x = 0 >>> line = infile.readline() >>> while True: line = infile.readline() if not line: break counter = counter + 1 States, OJ = string.split(line) XSTATES = str(States) XNUM = int(OJ) state_name.append(XSTATES) data_value.append(XNUM) >>> type(data_value) >>> print(data_value) [] >>> print (line) All I get is errors from here on out. LN States OJ AK 36 AL 39 AR 39 AZ 45 CA 61 CO 54 CT 61 DC 93 DE 62 FL 51 GA 47 HI 72 IA 54 ID 36 IL 62 IN 50 KS 41 KY 41 LA 40 MA 62 MD 62 ME 58 MI 57 MN 54 MO 49 MS 43 MT 47 NC 50 ND 45 NE 41 NH 54 NJ 57 NM 57 NV 55 NY 62 OH 51 OK 34 OR 57 PA 55 RI 63 SC 45 SD 45 TN 42 TX 44 UT 34 VA 53 VT 67 WA 58 WI 56 WV 43 WY 33 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Read, Write, Split and Append lines from a text file
Please include the complete stack trace error message It will help in determining what the error is. Jay On 07/19/2014 05:27 PM, LN A-go-go wrote: I have given it all I got: a week trying to open, read, write, split, append, convert to integers and then manipulate the data. I have attached the file I want to pull the names and numbers from. I'll cut and paste it incase that doesn't work and then include a sample of the python shell code. AK 36 AL 39 AR 39 AZ 45 CA 61 CO 54 CT 61 DC 93 DE 62 FL 51 GA 47 HI 72 IA 54 ID 36 IL 62 IN 50 KS 41 KY 41 LA 40 MA 62 MD 62 ME 58 MI 57 MN 54 MO 49 MS 43 MT 47 NC 50 ND 45 NE 41 NH 54 NJ 57 NM 57 NV 55 NY 62 OH 51 OK 34 OR 57 PA 55 RI 63 SC 45 SD 45 TN 42 TX 44 UT 34 VA 53 VT 67 WA 58 WI 56 WV 43 WY 33 # and now my newbie attempt: # I can get this far in approach #1 >>> filename = "z:/Geog482/egund919/Program1/BOJ.txt" >>> myfile = open(filename,"r") >>> newfile = "z:/Geog482/egund919/Program1/BOJ_B.txt" >>> mynewfile = open(newfile,"w") >>> while True: line = myfile.readline() print line mynewfile.write(line) if not line: break States OJ AK 36 AL 39 AR 39 AZ 45 CA 61 CO 54 CT 61 DC 93 DE 62 FL 51 GA 47 HI 72 IA 54 ID 36 IL 62 IN 50 KS 41 KY 41 LA 40 MA 62 MD 62 ME 58 MI 57 MN 54 MO 49 MS 43 MT 47 NC 50 ND 45 NE 41 NH 54 NJ 57 NM 57 NV 55 NY 62 OH 51 OK 34 OR 57 PA 55 RI 63 SC 45 SD 45 TN 42 TX 44 UT 34 VA 53 VT 67 WA 58 WI 56 WV 43 WY 33 In approach number 2: trying to append, convert to integer, but the list comes up empty: >>> infile = open('C:/Python27/egund919/Program1/BOJ_B.txt','r') >>> import string >>> state_name = [] >>> data_value = [] >>> counter = 0 >>> x = 0 >>> line = infile.readline() >>> while True: line = infile.readline() if not line: break counter = counter + 1 States, OJ = string.split(line) XSTATES = str(States) XNUM = int(OJ) state_name.append(XSTATES) data_value.append(XNUM) >>> type(data_value) >>> print(data_value) [] >>> print (line) All I get is errors from here on out. LN ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor -- Jay Lozier jsloz...@gmail.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Read, Write, Split and Append lines from a text file
On 20/07/2014 00:59, Jay Lozier wrote: Please include the complete stack trace error message It will help in determining what the error is. Jay -- Jay Lozier jsloz...@gmail.com Please don't top post here. Did you have to include the entire message just to include a two sentence reply? -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Read, Write, Split and Append lines from a text file
On Sat, Jul 19, 2014 at 2:27 PM, LN A-go-go wrote: > > > I have given it all I got: a week trying to open, read, write, split, append, > convert to integers and then manipulate the data. I have attached the file I > want to pull the names and numbers from. I'll cut and paste it incase that > doesn't work and then include a sample of the python shell code. > [cut] Sounds good so far. > # and now my newbie attempt: > # I can get this far in approach #1 > >>> filename = "z:/Geog482/egund919/Program1/BOJ.txt" > >>> myfile = open(filename,"r") > >>> newfile = "z:/Geog482/egund919/Program1/BOJ_B.txt" > >>> mynewfile = open(newfile,"w") > >>> while True: > line = myfile.readline() > print line > mynewfile.write(line) > if not line: break After you have written all these lines to 'newfile', did you call close() on "newfile"? I was expecting: mynewfile.close() at the end of this block; the fact that it's missing looks unusual to me. It may matter. File buffering may prevent another read of the file from seeing the output you've written until the file has been committed to disk. > In approach number 2: trying to append, convert to integer, but the list > comes up empty: Given the logic of the block, there are a few ways that the data_value may be empty. Let's take a look at it again. infile = open('C:/Python27/egund919/Program1/BOJ_B.txt','r') # ... data_value = [] # ... while True: line = infile.readline() if not line: break # some content cut data_value.append(...) I've omitted all but what I think is essential for analyzing this problem. I believe that the only way that data_value can be empty, given the control flow of this program, if is the infile.readline() returns a false value. The suggestion above about making sure the file has been committed to disk is meant to address this possibility. By the way, the looping over the file, using readline(), is stylistically unusual. Files are "iterable": they can be used in for loops directly. That is, the block: infile = open('C:/Python27/egund919/Program1/BOJ_B.txt','r') while True: line = infile.readline() ... can be written as: infile = open('C:/Python27/egund919/Program1/BOJ_B.txt','r') for line in infile: ... and in general, this is preferable to the explicit readline() approach you're using to iterate over the lines of a file. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Read, Write, Split and Append lines from a text file
LN A-go-go Wrote in message: By trying to include an attachment, you make it impossible for me to quote your actual message. Please use plain text message without attachments, to maximize the number of people that can read and readily respond. > StatesOJ > AK36 > AL39 > AR39 > AZ45 5 lines should be enough to get the idea. You don't specify the environment, so I'll have to assume Python 3.x. And it is likely Windows since you use x: as though it were part of the path. > You'll have to pretend this is properly quoted. ¥ and now my newbie attempt: # I can get this far in approach #1 >>> filename = "z:/Geog482/egund919/Program1/BOJ.txt" >>> myfile = open(filename,"r") >>> newfile = "z:/Geog482/egund919/Program1/BOJ_B.txt" >>> mynewfile = open(newfile,"w") >>> while True: line = myfile.readline() print line mynewfile.write(line) if not line: break ₩₩₩ print by itself is a no-op. If you had included the parentheses, it'd at least produce a blank line. line by itself is also a no-op. What were you trying to do with the variable? You neglected to close either of the files. Won't probably matter here, but it will someday. Best get the habit now. Just what was the point of this fragment? In what way did it fall short, other than having two no-ops in it? You have a different fragment with other apparent problems. But you never spell out the problem. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor