Re: Python path and append
On Sat, 30 Apr 2016 06:26 am, boB Stepp wrote: > On Mon, Apr 25, 2016 at 8:51 PM, Steven D'Aprano> wrote: > >> See here for the *start* of a more professional approach: >> >> http://code.activestate.com/recipes/579097-safely-and-atomically-write-to-a-file/ > > What else would I need to know/consider in order to have a *complete* > professional approach? When I know, I'll tell you. I think the answer depends in part on what you're trying to do. The requirements for, say, an ACID-compliant database are much heavier than for most applications. As far as regular applications go, I'd like to see: - the above tested and debugged for Windows and MacOS; - should it save the previous version of the file? - if so, how many backup files should it keep? - should it try to sync and flush the disk on saving the file? - anything I haven't thought of? Some of those (like the keeping of backups) is probably something that should be given as an option to the end-user. -- Steven -- https://mail.python.org/mailman/listinfo/python-list
Re: Python path and append
On Mon, Apr 25, 2016 at 8:51 PM, Steven D'Apranowrote: > See here for the *start* of a more professional approach: > > http://code.activestate.com/recipes/579097-safely-and-atomically-write-to-a-file/ What else would I need to know/consider in order to have a *complete* professional approach? -- boB -- https://mail.python.org/mailman/listinfo/python-list
Re: Python path and append
On Tue, Apr 26, 2016, at 07:56 PM, Seymore4Head wrote: > On Tue, 26 Apr 2016 11:53:57 +1000, Steven D'Aprano >wrote: > > >On Tue, 26 Apr 2016 08:04 am, Seymore4Head wrote: > > > >> BTW I was trying to use a line like yours that used an output file > >> that didn't exist and was getting an error. I assume that import os > >> fixes that. > > > > > >Why would you assume that? > > > > > >"Doctor, I have a problem with my arm, but I won't tell you what. I assume > >that if I take cough drops that will fix it." > > OK. Dumb question acknowledged. > > I got an error when I tried to write to a non existent file. I > "incorrectly" assumed wrong. That is going to be a common theme. Oh, he wasn't saying it was a dumb question. He was complaining you said you were "getting an error". That's not a dumb question, that's a useless report of a problem. If you don't say what exactly the error is, we can't help you. If you want help, the two best things to do are: 1) Show actual code. 2) Show actual, complete tracebacks. Having a nice description of what you expect to happen is often nice too, especially if its doing something "wrong" and not giving an obvious traceback. Seeing specifically what the wrong behavior is, and you explaining why you think its wrong, can be invaluable. -- Stephen Hansen m e @ i x o k a i . i o -- https://mail.python.org/mailman/listinfo/python-list
Re: Python path and append
On Wed, Apr 27, 2016 at 12:56 PM, Seymore4Headwrote: > On Tue, 26 Apr 2016 11:53:57 +1000, Steven D'Aprano > wrote: > >>On Tue, 26 Apr 2016 08:04 am, Seymore4Head wrote: >> >>> BTW I was trying to use a line like yours that used an output file >>> that didn't exist and was getting an error. I assume that import os >>> fixes that. >> >> >>Why would you assume that? >> >> >>"Doctor, I have a problem with my arm, but I won't tell you what. I assume >>that if I take cough drops that will fix it." > > OK. Dumb question acknowledged. > > I got an error when I tried to write to a non existent file. I > "incorrectly" assumed wrong. That is going to be a common theme. If you can acknowledge that, I suggest adopting a stance that will tend to avoid such assumptions in the future. The easiest way is to do less of your own analysis and more copy/paste of exact error messages. Don't say "getting an error" - quote the actual code and error. Don't say "I assume that", ask the question ("Would importing the os module fix that?"). We'll be able to help more easily with actual error text, and you won't look dumb :) ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Python path and append
On Tue, 26 Apr 2016 19:16:56 +0100, Michaelwrote: >If you want to read an entire file, append a space and asterisk and write it >to another file, this is the code you need: > >infile = open('win.txt', 'r') >text = f.read() >infile.close() >text += " *" >outfile = open('outfile.txt', 'w') >outfile.write(text) >outfile.close() > >If, on the other hand, you wish to read a file and append a space and asterisk >TO THE END OF EVERY LINE, you require the following code: >infile = open('win.txt', 'r') >lines = infile.readlines() >infile.close() >outfile = open('outfile.txt', 'w') >for line in lines: >line = line.strip() + " *\n" >outfile.write(line) >outfile.close() > >Hope that helps! > >BigBadMick >bigbadmick2...@hotmail.com I will have a look at this. Thanks -- https://mail.python.org/mailman/listinfo/python-list
Re: Python path and append
On Tue, 26 Apr 2016 11:53:57 +1000, Steven D'Apranowrote: >On Tue, 26 Apr 2016 08:04 am, Seymore4Head wrote: > >> BTW I was trying to use a line like yours that used an output file >> that didn't exist and was getting an error. I assume that import os >> fixes that. > > >Why would you assume that? > > >"Doctor, I have a problem with my arm, but I won't tell you what. I assume >that if I take cough drops that will fix it." OK. Dumb question acknowledged. I got an error when I tried to write to a non existent file. I "incorrectly" assumed wrong. That is going to be a common theme. -- https://mail.python.org/mailman/listinfo/python-list
Re: Python path and append
If you want to read an entire file, append a space and asterisk and write it to another file, this is the code you need: infile = open('win.txt', 'r') text = f.read() infile.close() text += " *" outfile = open('outfile.txt', 'w') outfile.write(text) outfile.close() If, on the other hand, you wish to read a file and append a space and asterisk TO THE END OF EVERY LINE, you require the following code: infile = open('win.txt', 'r') lines = infile.readlines() infile.close() outfile = open('outfile.txt', 'w') for line in lines: line = line.strip() + " *\n" outfile.write(line) outfile.close() Hope that helps! BigBadMick bigbadmick2...@hotmail.com -- https://mail.python.org/mailman/listinfo/python-list
Re: Python path and append
Steven D'Aprano wrote: (Possible a few very old operating systems on supercomputers from the 1970s or 80s may have supported inserting... I seem to recall that VMS may have allowed that... but don't quote me.) I wouldn't be surprised if VMS provided some sort of indexed random-access file structure that supported inserting records. OSes of that era tended to be big on things like that, since they were all about Serious Data Processing. But it probably wouldn't have let you insert data into the middle of a sequential file without explicitly moving everything that came after it. -- Greg -- https://mail.python.org/mailman/listinfo/python-list
Re: Python path and append
On Tue, 26 Apr 2016 11:51:23 +1000, Steven D'Aprano wrote: > ... (Possible a few very old operating systems on supercomputers from > the 1970s or 80s may have supported inserting... I seem to recall that > VMS may have allowed that... but don't quote me.) Some [non-supercomputer] OSes/filesystems/languages support(ed) the notion of fixed-length records, where a file acted as if it were an array of records. You could, e.g., replace Record Number 3 without disturbing Record Number 2 or Record Number 4. In the systems I remember like that, the records were simply ad hoc collections of whatever you wrote, not unlike the arguments to print, and it was up to your application to read and write the same structure. The limitation, of course, was that you had to declare the length up front, and that you ended up with wasted space somewhere if a lot of your records were "short." -- https://mail.python.org/mailman/listinfo/python-list
Re: Python path and append
On Tue, 26 Apr 2016 08:04 am, Seymore4Head wrote: > BTW I was trying to use a line like yours that used an output file > that didn't exist and was getting an error. I assume that import os > fixes that. Why would you assume that? "Doctor, I have a problem with my arm, but I won't tell you what. I assume that if I take cough drops that will fix it." -- Steven -- https://mail.python.org/mailman/listinfo/python-list
Re: Python path and append
On Tue, 26 Apr 2016 05:00 am, Seymore4Head wrote: > I was reading that. I have read it before. I don't use python enough > to even remember the simple stuff. Then when I try to use if for > something simple I forget how. It is perfectly fine to forget things that you read weeks or months before. The point is, having forgotten it, you should go back and refresh your memory when you need to. > f = open('wout.txt', 'r+') > for line in f: > line=line.strip() > f.write(line+" *") > f.close() > > Still broke. How about just telling me where I missed? Please? The contents of files don't just magically get moved out of the way when you write to them. There is no "insert mode" for files -- they are always set to "overwrite" mode. (Possible a few very old operating systems on supercomputers from the 1970s or 80s may have supported inserting... I seem to recall that VMS may have allowed that... but don't quote me.) So you can append to the *end* of a file without disrupting the content, but you cannot insert to the middle or beginning of a file without overwriting. The basic way to insert data into a file is: * read the entire file into memory; * insert the new data into the memory; * write the entire file out again. with open("myfile.txt") as f: lines = f.readlines() for i, line in enumerate(lines): lines[i] = line.strip() + " *\n" # space, asterisk, newline with open("myfile.txt", "w") as f: f.writelines(lines) That's good enough for DIY or hobby use. But for professional use, it starts getting *very* complex quickly. What if the power goes off or your computer crashes half-way through writing the file? You've lost all your data. See here for the *start* of a more professional approach: http://code.activestate.com/recipes/579097-safely-and-atomically-write-to-a-file/ -- Steven -- https://mail.python.org/mailman/listinfo/python-list
Re: Python path and append
On Tue, Apr 26, 2016 at 7:26 AM, John Gordonwrote: > It's much easier to create a new file and then rename it afterwards, > instead of rewriting the original file. And more importantly, it's safer. If anything happens to your process while it's doing its work, you'll have a junk file sitting around, but you won't have lost everything. If you overwrite the existing file, you (often) depend on completely writing out the content you have in memory. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Python path and append
On Mon, 25 Apr 2016 21:26:34 + (UTC), John Gordonwrote: >In <27nshbp40p1llr231dqm31p754tvurk...@4ax.com> Seymore4Head > writes: > >> On Tue, 19 Apr 2016 18:29:38 -0400, Seymore4Head >> wrote: > >> I am going to forget using a directory path. >> I would like to take the file win.txt and append a space and the * >> symbol. > >> f = open('win.txt', 'r+') >> for line in f: >> f.read(line) >> f.write(line+" *") > >> This doesn't work. Would someone fix it please? It is for a task I >> am trying to accomplish just for a home task. > >It's much easier to create a new file and then rename it afterwards, >instead of rewriting the original file. > >import os > >f_in = open('win.txt', 'r') >f_out = open('win_new.txt', 'w') > >for line in f_in.read().splitlines(): >f_out.write(line + " *\n") > >f_in.close() >f_out.close() > >os.rename('win.txt', 'win_old.txt') >os.rename('win_new.txt', 'win.txt') That is 100% spoon fed code and that is what I wanted. Thanks I learned enough python to complete an online course from Dr Chuck but I don't write enough code to remember how except for a simple task that is usually simpler to do manually than to remember how to code. http://www.dr-chuck.com/ Thanks to everyone else too. BTW I was trying to use a line like yours that used an output file that didn't exist and was getting an error. I assume that import os fixes that. -- https://mail.python.org/mailman/listinfo/python-list
Re: Python path and append
Random832 wrote: > On Mon, Apr 25, 2016, at 16:15, Seymore4Head wrote: >> Thanks for the tip. >> >> Still broke. :( >> >> f = open('wout.txt', 'r+') >> for line in f: >> if line=="": >> exit >> line=line[:-1] >> line=line+" *" >> f.write(line) >> print line >> f.close() > > Your problem is that after you read the first line, your file "cursor" > is positioned after the end of that line. So when you write the modified > version of the line, it ends up after that. And then when you write it, > the cursor is wherever the end of that is. > > So if you start with this: > AAA > BBB > CCC > > You'll end up with this: > AAA > AAA* [this overwrites "BBB_C" with "AAA*_" if _ is the line break] > CC > CC* > > There's no good way around this. You can either read the whole file into > memory at once into a list, then rewind (look at the seek function) and > write the lines out of the list, or you can write to a *different* file > than the one you're reading. You can leave the details to python though: $ cat sample.txt alpha beta gamma $ python Python 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import fileinput >>> for line in fileinput.input("sample.txt", inplace=True): ... print line.rstrip("\n"), "*" ... >>> $ cat sample.txt alpha * beta * gamma * Too much magic for my taste, but the OP might like it. -- https://mail.python.org/mailman/listinfo/python-list
Re: Python path and append
In <27nshbp40p1llr231dqm31p754tvurk...@4ax.com> Seymore4Headwrites: > On Tue, 19 Apr 2016 18:29:38 -0400, Seymore4Head > wrote: > I am going to forget using a directory path. > I would like to take the file win.txt and append a space and the * > symbol. > f = open('win.txt', 'r+') > for line in f: > f.read(line) > f.write(line+" *") > This doesn't work. Would someone fix it please? It is for a task I > am trying to accomplish just for a home task. It's much easier to create a new file and then rename it afterwards, instead of rewriting the original file. import os f_in = open('win.txt', 'r') f_out = open('win_new.txt', 'w') for line in f_in.read().splitlines(): f_out.write(line + " *\n") f_in.close() f_out.close() os.rename('win.txt', 'win_old.txt') os.rename('win_new.txt', 'win.txt') -- John Gordon A is for Amy, who fell down the stairs gor...@panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Tinies" -- https://mail.python.org/mailman/listinfo/python-list
Re: Python path and append
I am using a test file that is only 3 lines: Punjabi .Mp3 Big Lake (DVD) SWV.avi Blue Balloon.AHC.RH.mkv The program correctly appends an * to the end of the line, but then it goes into a loop printing random looking stuff. f = open('wout.txt', 'r+') for line in f: if line=="": exit line=line[:-1] line=line+" *" f.write(line) print line f.close() On Mon, 25 Apr 2016 20:44:50 +0100, MRAB <pyt...@mrabarnett.plus.com> wrote: >On 2016-04-25 20:08, Joaquin Alzola wrote: >> Strip() = white spaces. >> Description >> The method strip() returns a copy of the string in which all chars have been >> stripped from the beginning and the end of the string (default whitespace >> characters). >> >> Use to remove return carriage--> line[:-1] >> >1. In the file it might be a linefeed, or a carriage return, or a >carriage return followed by a linefeed, depending on the operating >system. Python translates it to a linefeed "\n" (or 'newline') on >reading. > >2. It's possible that the last line doesn't end have a line ending, so >line[:-1] could be removing some other character. It's safer to use >line.rstrip("\n"). > >> -Original Message- >> From: Python-list >> [mailto:python-list-bounces+joaquin.alzola=lebara@python.org] On Behalf >> Of Seymore4Head >> Sent: 25 April 2016 20:01 >> To: python-list@python.org >> Subject: Re: Python path and append >> >> On Mon, 25 Apr 2016 18:24:02 - (UTC), Rob Gaddi >> <rgaddi@highlandtechnology.invalid> wrote: >> >>>Seymore4Head wrote: >>> >>>> On Tue, 19 Apr 2016 18:29:38 -0400, Seymore4Head >>>> <Seymore4Head@Hotmail.invalid> wrote: >>>> >>>> I am going to forget using a directory path. >>>> I would like to take the file win.txt and append a space and the * >>>> symbol. >>>> >>>> f = open('win.txt', 'r+') >>>> for line in f: >>>> f.read(line) >>>> f.write(line+" *") >>>> >>>> This doesn't work. Would someone fix it please? It is for a task I >>>> am trying to accomplish just for a home task. >>> >>>"for line in f:" already means "make the variable line equal to each >>>line in f sequentially". f.read is both superfluous and also doesn't >>>do that. Leave it out entirely. >>> >>>The next problem you'll have is that iterating over the lines of the >>>file leaves the newline at the end of line, so your * will end up on >>>the wrong line. >>> >>>Do yourself a favor: >>>https://docs.python.org/3/tutorial/inputoutput.html >>>isn't very long. >> >> I was reading that. I have read it before. I don't use python enough to >> even remember the simple stuff. Then when I try to use if for something >> simple I forget how. >> >> f = open('wout.txt', 'r+') >> for line in f: >> line=line.strip() >> f.write(line+" *") >> f.close() >> >> Still broke. How about just telling me where I missed? Please? >> -- >> https://mail.python.org/mailman/listinfo/python-list >> This email is confidential and may be subject to privilege. If you are not >> the intended recipient, please do not copy or disclose its content but >> contact the sender immediately upon receipt. >> -- https://mail.python.org/mailman/listinfo/python-list
Re: Python path and append
On Mon, Apr 25, 2016, at 16:15, Seymore4Head wrote: > Thanks for the tip. > > Still broke. :( > > f = open('wout.txt', 'r+') > for line in f: > if line=="": > exit > line=line[:-1] > line=line+" *" > f.write(line) > print line > f.close() Your problem is that after you read the first line, your file "cursor" is positioned after the end of that line. So when you write the modified version of the line, it ends up after that. And then when you write it, the cursor is wherever the end of that is. So if you start with this: AAA BBB CCC You'll end up with this: AAA AAA* [this overwrites "BBB_C" with "AAA*_" if _ is the line break] CC CC* There's no good way around this. You can either read the whole file into memory at once into a list, then rewind (look at the seek function) and write the lines out of the list, or you can write to a *different* file than the one you're reading. -- https://mail.python.org/mailman/listinfo/python-list
Re: Python path and append
Thanks for the tip. Still broke. :( f = open('wout.txt', 'r+') for line in f: if line=="": exit line=line[:-1] line=line+" *" f.write(line) print line f.close() I did notice that it wrote the 3 lines of test file but it didn't append the * after the third entry and it starts printing garbage after that. On Mon, 25 Apr 2016 19:08:56 +, Joaquin Alzola <joaquin.alz...@lebara.com> wrote: >Strip() = white spaces. >Description >The method strip() returns a copy of the string in which all chars have been >stripped from the beginning and the end of the string (default whitespace >characters). > >Use to remove return carriage--> line[:-1] > >-Original Message- >From: Python-list >[mailto:python-list-bounces+joaquin.alzola=lebara@python.org] On Behalf Of >Seymore4Head >Sent: 25 April 2016 20:01 >To: python-list@python.org >Subject: Re: Python path and append > >On Mon, 25 Apr 2016 18:24:02 - (UTC), Rob Gaddi ><rgaddi@highlandtechnology.invalid> wrote: > >>Seymore4Head wrote: >> >>> On Tue, 19 Apr 2016 18:29:38 -0400, Seymore4Head >>> <Seymore4Head@Hotmail.invalid> wrote: >>> >>> I am going to forget using a directory path. >>> I would like to take the file win.txt and append a space and the * >>> symbol. >>> >>> f = open('win.txt', 'r+') >>> for line in f: >>> f.read(line) >>> f.write(line+" *") >>> >>> This doesn't work. Would someone fix it please? It is for a task I >>> am trying to accomplish just for a home task. >> >>"for line in f:" already means "make the variable line equal to each >>line in f sequentially". f.read is both superfluous and also doesn't >>do that. Leave it out entirely. >> >>The next problem you'll have is that iterating over the lines of the >>file leaves the newline at the end of line, so your * will end up on >>the wrong line. >> >>Do yourself a favor: >>https://docs.python.org/3/tutorial/inputoutput.html >>isn't very long. > >I was reading that. I have read it before. I don't use python enough to even >remember the simple stuff. Then when I try to use if for something simple I >forget how. > >f = open('wout.txt', 'r+') >for line in f: >line=line.strip() >f.write(line+" *") >f.close() > >Still broke. How about just telling me where I missed? Please? -- https://mail.python.org/mailman/listinfo/python-list
Re: Python path and append
On 2016-04-25 20:08, Joaquin Alzola wrote: Strip() = white spaces. Description The method strip() returns a copy of the string in which all chars have been stripped from the beginning and the end of the string (default whitespace characters). Use to remove return carriage--> line[:-1] 1. In the file it might be a linefeed, or a carriage return, or a carriage return followed by a linefeed, depending on the operating system. Python translates it to a linefeed "\n" (or 'newline') on reading. 2. It's possible that the last line doesn't end have a line ending, so line[:-1] could be removing some other character. It's safer to use line.rstrip("\n"). -Original Message- From: Python-list [mailto:python-list-bounces+joaquin.alzola=lebara@python.org] On Behalf Of Seymore4Head Sent: 25 April 2016 20:01 To: python-list@python.org Subject: Re: Python path and append On Mon, 25 Apr 2016 18:24:02 - (UTC), Rob Gaddi <rgaddi@highlandtechnology.invalid> wrote: Seymore4Head wrote: On Tue, 19 Apr 2016 18:29:38 -0400, Seymore4Head <Seymore4Head@Hotmail.invalid> wrote: I am going to forget using a directory path. I would like to take the file win.txt and append a space and the * symbol. f = open('win.txt', 'r+') for line in f: f.read(line) f.write(line+" *") This doesn't work. Would someone fix it please? It is for a task I am trying to accomplish just for a home task. "for line in f:" already means "make the variable line equal to each line in f sequentially". f.read is both superfluous and also doesn't do that. Leave it out entirely. The next problem you'll have is that iterating over the lines of the file leaves the newline at the end of line, so your * will end up on the wrong line. Do yourself a favor: https://docs.python.org/3/tutorial/inputoutput.html isn't very long. I was reading that. I have read it before. I don't use python enough to even remember the simple stuff. Then when I try to use if for something simple I forget how. f = open('wout.txt', 'r+') for line in f: line=line.strip() f.write(line+" *") f.close() Still broke. How about just telling me where I missed? Please? -- https://mail.python.org/mailman/listinfo/python-list This email is confidential and may be subject to privilege. If you are not the intended recipient, please do not copy or disclose its content but contact the sender immediately upon receipt. -- https://mail.python.org/mailman/listinfo/python-list
Re: Python path and append
Seymore4Head wrote: > On Mon, 25 Apr 2016 18:24:02 - (UTC), Rob Gaddi >wrote: > >>Seymore4Head wrote: >> >>> On Tue, 19 Apr 2016 18:29:38 -0400, Seymore4Head >>> wrote: >>> >>> I am going to forget using a directory path. >>> I would like to take the file win.txt and append a space and the * >>> symbol. >>> >>> f = open('win.txt', 'r+') >>> for line in f: >>> f.read(line) >>> f.write(line+" *") >>> >>> This doesn't work. Would someone fix it please? It is for a task I >>> am trying to accomplish just for a home task. >> >>"for line in f:" already means "make the variable line equal to each >>line in f sequentially". f.read is both superfluous and also doesn't do >>that. Leave it out entirely. >> >>The next problem you'll have is that iterating over the lines of the >>file leaves the newline at the end of line, so your * will end up on the >>wrong line. >> >>Do yourself a favor: https://docs.python.org/3/tutorial/inputoutput.html >>isn't very long. > > I was reading that. I have read it before. I don't use python enough > to even remember the simple stuff. Then when I try to use if for > something simple I forget how. > > f = open('wout.txt', 'r+') > for line in f: > line=line.strip() > f.write(line+" *") > f.close() > > Still broke. How about just telling me where I missed? Please? Depends on what "broke" means. I'm going to go out on a limb and guess that the problem now is that you get no newlines at all, because they've been stripped off of the line you read and .write doesn't put it back on, in which case you should be adding " *\n" instead. If that's not it, then reply hazy, please concentrate and ask again. -- Rob Gaddi, Highland Technology -- www.highlandtechnology.com Email address domain is currently out of order. See above to fix. -- https://mail.python.org/mailman/listinfo/python-list
RE: Python path and append
Strip() = white spaces. Description The method strip() returns a copy of the string in which all chars have been stripped from the beginning and the end of the string (default whitespace characters). Use to remove return carriage--> line[:-1] -Original Message- From: Python-list [mailto:python-list-bounces+joaquin.alzola=lebara@python.org] On Behalf Of Seymore4Head Sent: 25 April 2016 20:01 To: python-list@python.org Subject: Re: Python path and append On Mon, 25 Apr 2016 18:24:02 - (UTC), Rob Gaddi <rgaddi@highlandtechnology.invalid> wrote: >Seymore4Head wrote: > >> On Tue, 19 Apr 2016 18:29:38 -0400, Seymore4Head >> <Seymore4Head@Hotmail.invalid> wrote: >> >> I am going to forget using a directory path. >> I would like to take the file win.txt and append a space and the * >> symbol. >> >> f = open('win.txt', 'r+') >> for line in f: >> f.read(line) >> f.write(line+" *") >> >> This doesn't work. Would someone fix it please? It is for a task I >> am trying to accomplish just for a home task. > >"for line in f:" already means "make the variable line equal to each >line in f sequentially". f.read is both superfluous and also doesn't >do that. Leave it out entirely. > >The next problem you'll have is that iterating over the lines of the >file leaves the newline at the end of line, so your * will end up on >the wrong line. > >Do yourself a favor: >https://docs.python.org/3/tutorial/inputoutput.html >isn't very long. I was reading that. I have read it before. I don't use python enough to even remember the simple stuff. Then when I try to use if for something simple I forget how. f = open('wout.txt', 'r+') for line in f: line=line.strip() f.write(line+" *") f.close() Still broke. How about just telling me where I missed? Please? -- https://mail.python.org/mailman/listinfo/python-list This email is confidential and may be subject to privilege. If you are not the intended recipient, please do not copy or disclose its content but contact the sender immediately upon receipt. -- https://mail.python.org/mailman/listinfo/python-list
Re: Python path and append
On Mon, 25 Apr 2016 18:24:02 - (UTC), Rob Gaddiwrote: >Seymore4Head wrote: > >> On Tue, 19 Apr 2016 18:29:38 -0400, Seymore4Head >> wrote: >> >> I am going to forget using a directory path. >> I would like to take the file win.txt and append a space and the * >> symbol. >> >> f = open('win.txt', 'r+') >> for line in f: >> f.read(line) >> f.write(line+" *") >> >> This doesn't work. Would someone fix it please? It is for a task I >> am trying to accomplish just for a home task. > >"for line in f:" already means "make the variable line equal to each >line in f sequentially". f.read is both superfluous and also doesn't do >that. Leave it out entirely. > >The next problem you'll have is that iterating over the lines of the >file leaves the newline at the end of line, so your * will end up on the >wrong line. > >Do yourself a favor: https://docs.python.org/3/tutorial/inputoutput.html >isn't very long. I was reading that. I have read it before. I don't use python enough to even remember the simple stuff. Then when I try to use if for something simple I forget how. f = open('wout.txt', 'r+') for line in f: line=line.strip() f.write(line+" *") f.close() Still broke. How about just telling me where I missed? Please? -- https://mail.python.org/mailman/listinfo/python-list
Re: Python path and append
Seymore4Head wrote: > On Tue, 19 Apr 2016 18:29:38 -0400, Seymore4Head >wrote: > > I am going to forget using a directory path. > I would like to take the file win.txt and append a space and the * > symbol. > > f = open('win.txt', 'r+') > for line in f: > f.read(line) > f.write(line+" *") > > This doesn't work. Would someone fix it please? It is for a task I > am trying to accomplish just for a home task. "for line in f:" already means "make the variable line equal to each line in f sequentially". f.read is both superfluous and also doesn't do that. Leave it out entirely. The next problem you'll have is that iterating over the lines of the file leaves the newline at the end of line, so your * will end up on the wrong line. Do yourself a favor: https://docs.python.org/3/tutorial/inputoutput.html isn't very long. -- Rob Gaddi, Highland Technology -- www.highlandtechnology.com Email address domain is currently out of order. See above to fix. -- https://mail.python.org/mailman/listinfo/python-list
Re: Python path and append
On Tue, 19 Apr 2016 18:29:38 -0400, Seymore4Headwrote: I am going to forget using a directory path. I would like to take the file win.txt and append a space and the * symbol. f = open('win.txt', 'r+') for line in f: f.read(line) f.write(line+" *") This doesn't work. Would someone fix it please? It is for a task I am trying to accomplish just for a home task. -- https://mail.python.org/mailman/listinfo/python-list
Re: Python path and append
On 2016-04-19 23:38, Chris Angelico wrote: On Wed, Apr 20, 2016 at 8:29 AM, Seymore4Headwrote: handle = open("\\Winmx\New$\q.txt") for line in handle: line=line.strip() print line Traceback (most recent call last): File "\\Winmx\New$\add viewed.py", line 2, in handle = open("\\Winmx\New$\q.txt") IOError: [Errno 2] No such file or directory: '\\Winmx\\New$\\q.txt' What I would like to do is read a plain text file from a hidden network drive and append a space and the * character to the end of each line. Start with this: print("\\Winmx\New$\q.txt") If that doesn't do what you expect, it's possibly because you want to use a raw string literal to prevent the backslashes from being parsed. handle = open(r"\\Winmx\New$\q.txt") That might help you. (One clue that this is happening is that some of your backslashes got doubled in the error message.) when printed out: >>> print "\\Winmx\New$\q.txt" \Winmx\New$\q.txt That's a file 2 directories down on the current drive. -- https://mail.python.org/mailman/listinfo/python-list
Re: Python path and append
On Wed, Apr 20, 2016 at 8:29 AM, Seymore4Headwrote: > > handle = open("\\Winmx\New$\q.txt") > for line in handle: > line=line.strip() > print line > > > Traceback (most recent call last): > File "\\Winmx\New$\add viewed.py", line 2, in > handle = open("\\Winmx\New$\q.txt") > IOError: [Errno 2] No such file or directory: '\\Winmx\\New$\\q.txt' > > What I would like to do is read a plain text file from a hidden > network drive and append a space and the * character to the end of > each line. Start with this: print("\\Winmx\New$\q.txt") If that doesn't do what you expect, it's possibly because you want to use a raw string literal to prevent the backslashes from being parsed. handle = open(r"\\Winmx\New$\q.txt") That might help you. (One clue that this is happening is that some of your backslashes got doubled in the error message.) ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Python path and append
This doesn't work. Does Python recognize hidden directories? handle = open("\\Winmx\New$\q.txt") for line in handle: line=line.strip() print line Traceback (most recent call last): File "\\Winmx\New$\add viewed.py", line 2, in handle = open("\\Winmx\New$\q.txt") IOError: [Errno 2] No such file or directory: '\\Winmx\\New$\\q.txt' What I would like to do is read a plain text file from a hidden network drive and append a space and the * character to the end of each line. Would someone be so kind as to fill in the blanks to accomplish this please? -- https://mail.python.org/mailman/listinfo/python-list