Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #111144 > unrolled thread
| Started by | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| First post | 2016-07-05 18:05 -0400 |
| Last post | 2016-07-06 15:35 +1000 |
| Articles | 18 — 7 participants |
Back to article view | Back to comp.lang.python
Appending an asterisk to the end of each line Seymore4Head <Seymore4Head@Hotmail.invalid> - 2016-07-05 18:05 -0400
Re: Appending an asterisk to the end of each line Joel Goldstick <joel.goldstick@gmail.com> - 2016-07-05 18:27 -0400
Re: Appending an asterisk to the end of each line Seymore4Head <Seymore4Head@Hotmail.invalid> - 2016-07-05 18:29 -0400
Re: Appending an asterisk to the end of each line Joel Goldstick <joel.goldstick@gmail.com> - 2016-07-05 18:40 -0400
Re: Appending an asterisk to the end of each line Seymore4Head <Seymore4Head@Hotmail.invalid> - 2016-07-05 19:04 -0400
Re: Appending an asterisk to the end of each line Chris Angelico <rosuav@gmail.com> - 2016-07-06 09:38 +1000
Re: Appending an asterisk to the end of each line Seymore4Head <Seymore4Head@Hotmail.invalid> - 2016-07-05 19:49 -0400
Re: Appending an asterisk to the end of each line MRAB <python@mrabarnett.plus.com> - 2016-07-06 00:03 +0100
Re: Appending an asterisk to the end of each line Seymore4Head <Seymore4Head@Hotmail.invalid> - 2016-07-05 19:15 -0400
Re: Appending an asterisk to the end of each line Joel Goldstick <joel.goldstick@gmail.com> - 2016-07-05 19:15 -0400
Re: Appending an asterisk to the end of each line Seymore4Head <Seymore4Head@Hotmail.invalid> - 2016-07-05 19:29 -0400
Re: Appending an asterisk to the end of each line Seymore4Head <Seymore4Head@Hotmail.invalid> - 2016-07-05 19:45 -0400
Re: Appending an asterisk to the end of each line MRAB <python@mrabarnett.plus.com> - 2016-07-06 01:05 +0100
Re: Appending an asterisk to the end of each line Seymore4Head <Seymore4Head@Hotmail.invalid> - 2016-07-05 20:05 -0400
Re: Appending an asterisk to the end of each line MRAB <python@mrabarnett.plus.com> - 2016-07-06 03:15 +0100
Re: Appending an asterisk to the end of each line Steven D'Aprano <steve@pearwood.info> - 2016-07-06 12:35 +1000
Re: Appending an asterisk to the end of each line Larry Hudson <orgnut@yahoo.com> - 2016-07-05 21:37 -0700
Re: Appending an asterisk to the end of each line cs@zip.com.au - 2016-07-06 15:35 +1000
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2016-07-05 18:05 -0400 |
| Subject | Appending an asterisk to the end of each line |
| Message-ID | <acbonb5kk4g5vr68rlpipmrehekitpfb4b@4ax.com> |
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')
I just tried to reuse this program that was posted several months ago.
I am using a text flie that is about 200 lines long and have named it
win.txt. The file it creates when I run the program is win_new.txt
but it's empty.
[toc] | [next] | [standalone]
| From | Joel Goldstick <joel.goldstick@gmail.com> |
|---|---|
| Date | 2016-07-05 18:27 -0400 |
| Message-ID | <mailman.110.1467757653.2295.python-list@python.org> |
| In reply to | #111144 |
On Tue, Jul 5, 2016 at 6:05 PM, Seymore4Head
<Seymore4Head@hotmail.invalid> wrote:
> 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')
>
>
> I just tried to reuse this program that was posted several months ago.
> I am using a text flie that is about 200 lines long and have named it
> win.txt. The file it creates when I run the program is win_new.txt
> but it's empty.
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list
Are you running program in same folder as text file?
--
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays
[toc] | [prev] | [next] | [standalone]
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2016-07-05 18:29 -0400 |
| Message-ID | <s3donb9552u0v4rv63gdqvisq44otam9l9@4ax.com> |
| In reply to | #111145 |
On Tue, 5 Jul 2016 18:27:25 -0400, Joel Goldstick
<joel.goldstick@gmail.com> wrote:
>On Tue, Jul 5, 2016 at 6:05 PM, Seymore4Head
><Seymore4Head@hotmail.invalid> wrote:
>> 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')
>>
>>
>> I just tried to reuse this program that was posted several months ago.
>> I am using a text flie that is about 200 lines long and have named it
>> win.txt. The file it creates when I run the program is win_new.txt
>> but it's empty.
>>
>>
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>
>Are you running program in same folder as text file?
Yes. I just reinstalled Python. Python is not installed in the same
folder and I don't remember if it needs to have path entered.
[toc] | [prev] | [next] | [standalone]
| From | Joel Goldstick <joel.goldstick@gmail.com> |
|---|---|
| Date | 2016-07-05 18:40 -0400 |
| Message-ID | <mailman.111.1467758454.2295.python-list@python.org> |
| In reply to | #111146 |
On Tue, Jul 5, 2016 at 6:29 PM, Seymore4Head
<Seymore4Head@hotmail.invalid> wrote:
> On Tue, 5 Jul 2016 18:27:25 -0400, Joel Goldstick
> <joel.goldstick@gmail.com> wrote:
>
>>On Tue, Jul 5, 2016 at 6:05 PM, Seymore4Head
>><Seymore4Head@hotmail.invalid> wrote:
>>> 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')
>>>
>>>
>>> I just tried to reuse this program that was posted several months ago.
>>> I am using a text flie that is about 200 lines long and have named it
>>> win.txt. The file it creates when I run the program is win_new.txt
>>> but it's empty.
>>>
>>>
>>> --
>>> https://mail.python.org/mailman/listinfo/python-list
>>
>>Are you running program in same folder as text file?
>
> Yes. I just reinstalled Python. Python is not installed in the same
> folder and I don't remember if it needs to have path entered.
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list
So, if you type python, do you enter the python shell?
--
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays
[toc] | [prev] | [next] | [standalone]
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2016-07-05 19:04 -0400 |
| Message-ID | <freonb9ohgk9ghdtvlk0vnnfgi4i2mgrkj@4ax.com> |
| In reply to | #111147 |
On Tue, 5 Jul 2016 18:40:51 -0400, Joel Goldstick
<joel.goldstick@gmail.com> wrote:
>On Tue, Jul 5, 2016 at 6:29 PM, Seymore4Head
><Seymore4Head@hotmail.invalid> wrote:
>> On Tue, 5 Jul 2016 18:27:25 -0400, Joel Goldstick
>> <joel.goldstick@gmail.com> wrote:
>>
>>>On Tue, Jul 5, 2016 at 6:05 PM, Seymore4Head
>>><Seymore4Head@hotmail.invalid> wrote:
>>>> 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')
>>>>
>>>>
>>>> I just tried to reuse this program that was posted several months ago.
>>>> I am using a text flie that is about 200 lines long and have named it
>>>> win.txt. The file it creates when I run the program is win_new.txt
>>>> but it's empty.
>>>>
>>>>
>>>> --
>>>> https://mail.python.org/mailman/listinfo/python-list
>>>
>>>Are you running program in same folder as text file?
>>
>> Yes. I just reinstalled Python. Python is not installed in the same
>> folder and I don't remember if it needs to have path entered.
>>
>>
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>
>So, if you type python, do you enter the python shell?
I am using XP and launching the program from another drive/folder than
the boot drive.
The program has .py extension and the icon shows it is associated with
Python.
I tried "start run" and then typed python and it did show the dos box
with c:\python34/python.exe and the python shell.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2016-07-06 09:38 +1000 |
| Message-ID | <mailman.115.1467761936.2295.python-list@python.org> |
| In reply to | #111149 |
On Wed, Jul 6, 2016 at 9:04 AM, Seymore4Head <Seymore4Head@hotmail.invalid> wrote: > I am using XP and launching the program from another drive/folder than > the boot drive. > > The program has .py extension and the icon shows it is associated with > Python. > > I tried "start run" and then typed python and it did show the dos box > with c:\python34/python.exe and the python shell. Okay. Step 1: Get a better operating system than Windows XP. Either upgrade to a newer version of Windows, or (my recommended option) switch to something like Debian Linux. Step 2, whether or not you do step 1: Become familiar with the command line. If you're staying with Windows, that means Start, Run, cmd.exe. Run your program from the terminal, and (this is the most important part) *copy and paste* its output into your next email asking for help. My suspicion is that your script is terminating with an exception somewhere, and you're not seeing it because the Windows association system gives you a Python that comes up and then disappears promptly when the program terminates. Step 3: If the above hasn't trivially solved your problem, now it's time for actual debugging work. Pepper your code with print() calls, showing the values of various things, until you figure out what's going on. Have fun! ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2016-07-05 19:49 -0400 |
| Message-ID | <orhonbl0jpjtiv7lsvh9eqsf7j0no9ed5t@4ax.com> |
| In reply to | #111155 |
On Wed, 6 Jul 2016 09:38:47 +1000, Chris Angelico <rosuav@gmail.com> wrote: >On Wed, Jul 6, 2016 at 9:04 AM, Seymore4Head ><Seymore4Head@hotmail.invalid> wrote: >> I am using XP and launching the program from another drive/folder than >> the boot drive. >> >> The program has .py extension and the icon shows it is associated with >> Python. >> >> I tried "start run" and then typed python and it did show the dos box >> with c:\python34/python.exe and the python shell. > >Okay. Step 1: Get a better operating system than Windows XP. Either >upgrade to a newer version of Windows, or (my recommended option) >switch to something like Debian Linux. > >Step 2, whether or not you do step 1: Become familiar with the command >line. If you're staying with Windows, that means Start, Run, cmd.exe. >Run your program from the terminal, and (this is the most important >part) *copy and paste* its output into your next email asking for >help. My suspicion is that your script is terminating with an >exception somewhere, and you're not seeing it because the Windows >association system gives you a Python that comes up and then >disappears promptly when the program terminates. > >Step 3: If the above hasn't trivially solved your problem, now it's >time for actual debugging work. Pepper your code with print() calls, >showing the values of various things, until you figure out what's >going on. > >Have fun! > >ChrisA Thanks for the suggestions.
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2016-07-06 00:03 +0100 |
| Message-ID | <mailman.112.1467759820.2295.python-list@python.org> |
| In reply to | #111144 |
On 2016-07-05 23:05, Seymore4Head wrote:
> 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')
>
>
> I just tried to reuse this program that was posted several months ago.
> I am using a text flie that is about 200 lines long and have named it
> win.txt. The file it creates when I run the program is win_new.txt
> but it's empty.
>
Although it creates a file called "win_new.txt", it then renames it to
"win.txt", so "win_new.txt" shouldn't exist.
Of course, if there's already a file called "win_old.txt", then the
first rename will raise an exception, and you'll have "win_new.txt" and
the original "win.txt".
[toc] | [prev] | [next] | [standalone]
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2016-07-05 19:15 -0400 |
| Message-ID | <7pfonb5k9h6uahq8ncfhrat411rjld6k63@4ax.com> |
| In reply to | #111148 |
On Wed, 6 Jul 2016 00:03:29 +0100, MRAB <python@mrabarnett.plus.com>
wrote:
>On 2016-07-05 23:05, Seymore4Head wrote:
>> 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')
>>
>>
>> I just tried to reuse this program that was posted several months ago.
>> I am using a text flie that is about 200 lines long and have named it
>> win.txt. The file it creates when I run the program is win_new.txt
>> but it's empty.
>>
>Although it creates a file called "win_new.txt", it then renames it to
>"win.txt", so "win_new.txt" shouldn't exist.
>
>Of course, if there's already a file called "win_old.txt", then the
>first rename will raise an exception, and you'll have "win_new.txt" and
>the original "win.txt".
When I run the program it creates a file called win_new.txt and
win.txt remains unchanged.
[toc] | [prev] | [next] | [standalone]
| From | Joel Goldstick <joel.goldstick@gmail.com> |
|---|---|
| Date | 2016-07-05 19:15 -0400 |
| Message-ID | <mailman.114.1467760532.2295.python-list@python.org> |
| In reply to | #111144 |
On Tue, Jul 5, 2016 at 7:03 PM, MRAB <python@mrabarnett.plus.com> wrote:
> On 2016-07-05 23:05, Seymore4Head wrote:
>>
>> 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')
>>
>>
>> I just tried to reuse this program that was posted several months ago.
>> I am using a text flie that is about 200 lines long and have named it
>> win.txt. The file it creates when I run the program is win_new.txt
>> but it's empty.
>>
> Although it creates a file called "win_new.txt", it then renames it to
> "win.txt", so "win_new.txt" shouldn't exist.
>
> Of course, if there's already a file called "win_old.txt", then the first
> rename will raise an exception, and you'll have "win_new.txt" and the
> original "win.txt".
>
> --
> https://mail.python.org/mailman/listinfo/python-list
Why don't you comment out the renames, and see what happens?
--
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays
[toc] | [prev] | [next] | [standalone]
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2016-07-05 19:29 -0400 |
| Message-ID | <7ggonbtg4vhul20v11qvsiufcks1dvouop@4ax.com> |
| In reply to | #111152 |
On Tue, 5 Jul 2016 19:15:23 -0400, Joel Goldstick
<joel.goldstick@gmail.com> wrote:
>On Tue, Jul 5, 2016 at 7:03 PM, MRAB <python@mrabarnett.plus.com> wrote:
>> On 2016-07-05 23:05, Seymore4Head wrote:
>>>
>>> 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')
>>>
>>>
>>> I just tried to reuse this program that was posted several months ago.
>>> I am using a text flie that is about 200 lines long and have named it
>>> win.txt. The file it creates when I run the program is win_new.txt
>>> but it's empty.
>>>
>> Although it creates a file called "win_new.txt", it then renames it to
>> "win.txt", so "win_new.txt" shouldn't exist.
>>
>> Of course, if there's already a file called "win_old.txt", then the first
>> rename will raise an exception, and you'll have "win_new.txt" and the
>> original "win.txt".
>>
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>
>Why don't you comment out the renames, and see what happens?
I really don't care if the filename gets renamed or not. I commented
out the renames, but I still get a new file called win_new.txt and it
is empty.
The original is unchanged.
[toc] | [prev] | [next] | [standalone]
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2016-07-05 19:45 -0400 |
| Message-ID | <tehonbhpoe0m2ies2dcnr2502iiq2ecltf@4ax.com> |
| In reply to | #111154 |
On Tue, 05 Jul 2016 19:29:21 -0400, Seymore4Head
<Seymore4Head@Hotmail.invalid> wrote:
>On Tue, 5 Jul 2016 19:15:23 -0400, Joel Goldstick
><joel.goldstick@gmail.com> wrote:
>
>>On Tue, Jul 5, 2016 at 7:03 PM, MRAB <python@mrabarnett.plus.com> wrote:
>>> On 2016-07-05 23:05, Seymore4Head wrote:
>>>>
>>>> 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')
>>>>
>>>>
>>>> I just tried to reuse this program that was posted several months ago.
>>>> I am using a text flie that is about 200 lines long and have named it
>>>> win.txt. The file it creates when I run the program is win_new.txt
>>>> but it's empty.
>>>>
>>> Although it creates a file called "win_new.txt", it then renames it to
>>> "win.txt", so "win_new.txt" shouldn't exist.
>>>
>>> Of course, if there's already a file called "win_old.txt", then the first
>>> rename will raise an exception, and you'll have "win_new.txt" and the
>>> original "win.txt".
>>>
>>> --
>>> https://mail.python.org/mailman/listinfo/python-list
>>
>>Why don't you comment out the renames, and see what happens?
>
>I really don't care if the filename gets renamed or not. I commented
>out the renames, but I still get a new file called win_new.txt and it
>is empty.
>
>The original is unchanged.
I just tried this on a 3 line text file and it works.
I am looking through the text file and have found at least two
suspicious characters. One is a German letter and the other is a
characters that has been replaced by a square symbol.
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2016-07-06 01:05 +0100 |
| Message-ID | <mailman.116.1467763523.2295.python-list@python.org> |
| In reply to | #111156 |
On 2016-07-06 00:45, Seymore4Head wrote:
> On Tue, 05 Jul 2016 19:29:21 -0400, Seymore4Head
> <Seymore4Head@Hotmail.invalid> wrote:
>
>>On Tue, 5 Jul 2016 19:15:23 -0400, Joel Goldstick
>><joel.goldstick@gmail.com> wrote:
>>
>>>On Tue, Jul 5, 2016 at 7:03 PM, MRAB <python@mrabarnett.plus.com> wrote:
>>>> On 2016-07-05 23:05, Seymore4Head wrote:
>>>>>
>>>>> 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')
>>>>>
>>>>>
>>>>> I just tried to reuse this program that was posted several months ago.
>>>>> I am using a text flie that is about 200 lines long and have named it
>>>>> win.txt. The file it creates when I run the program is win_new.txt
>>>>> but it's empty.
>>>>>
>>>> Although it creates a file called "win_new.txt", it then renames it to
>>>> "win.txt", so "win_new.txt" shouldn't exist.
>>>>
>>>> Of course, if there's already a file called "win_old.txt", then the first
>>>> rename will raise an exception, and you'll have "win_new.txt" and the
>>>> original "win.txt".
>>>>
>>>> --
>>>> https://mail.python.org/mailman/listinfo/python-list
>>>
>>>Why don't you comment out the renames, and see what happens?
>>
>>I really don't care if the filename gets renamed or not. I commented
>>out the renames, but I still get a new file called win_new.txt and it
>>is empty.
>>
>>The original is unchanged.
>
> I just tried this on a 3 line text file and it works.
>
> I am looking through the text file and have found at least two
> suspicious characters. One is a German letter and the other is a
> characters that has been replaced by a square symbol.
>
That suggests to me that it's an encoding problem (the traceback
would've indicated that).
Specify an encoding when you open the files:
f_in = open('win.txt', 'r', encoding='utf-8')
f_out = open('win_new.txt', 'w', encoding='utf-8')
assuming that 'win.txt' is indeed encoded in UTF-8. (It might be
something like ISO-8859-1 instead.)
[toc] | [prev] | [next] | [standalone]
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2016-07-05 20:05 -0400 |
| Message-ID | <goionbtk49q4dhh6p5fhpfd4ecuhsbpmbe@4ax.com> |
| In reply to | #111158 |
On Wed, 6 Jul 2016 01:05:12 +0100, MRAB <python@mrabarnett.plus.com>
wrote:
>On 2016-07-06 00:45, Seymore4Head wrote:
>> On Tue, 05 Jul 2016 19:29:21 -0400, Seymore4Head
>> <Seymore4Head@Hotmail.invalid> wrote:
>>
>>>On Tue, 5 Jul 2016 19:15:23 -0400, Joel Goldstick
>>><joel.goldstick@gmail.com> wrote:
>>>
>>>>On Tue, Jul 5, 2016 at 7:03 PM, MRAB <python@mrabarnett.plus.com> wrote:
>>>>> On 2016-07-05 23:05, Seymore4Head wrote:
>>>>>>
>>>>>> 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')
>>>>>>
>>>>>>
>>>>>> I just tried to reuse this program that was posted several months ago.
>>>>>> I am using a text flie that is about 200 lines long and have named it
>>>>>> win.txt. The file it creates when I run the program is win_new.txt
>>>>>> but it's empty.
>>>>>>
>>>>> Although it creates a file called "win_new.txt", it then renames it to
>>>>> "win.txt", so "win_new.txt" shouldn't exist.
>>>>>
>>>>> Of course, if there's already a file called "win_old.txt", then the first
>>>>> rename will raise an exception, and you'll have "win_new.txt" and the
>>>>> original "win.txt".
>>>>>
>>>>> --
>>>>> https://mail.python.org/mailman/listinfo/python-list
>>>>
>>>>Why don't you comment out the renames, and see what happens?
>>>
>>>I really don't care if the filename gets renamed or not. I commented
>>>out the renames, but I still get a new file called win_new.txt and it
>>>is empty.
>>>
>>>The original is unchanged.
>>
>> I just tried this on a 3 line text file and it works.
>>
>> I am looking through the text file and have found at least two
>> suspicious characters. One is a German letter and the other is a
>> characters that has been replaced by a square symbol.
>>
>That suggests to me that it's an encoding problem (the traceback
>would've indicated that).
>
>Specify an encoding when you open the files:
>
>f_in = open('win.txt', 'r', encoding='utf-8')
>f_out = open('win_new.txt', 'w', encoding='utf-8')
>
>assuming that 'win.txt' is indeed encoded in UTF-8. (It might be
>something like ISO-8859-1 instead.)
Thanks.
It is working now that I removed those two characters.
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2016-07-06 03:15 +0100 |
| Message-ID | <mailman.117.1467771316.2295.python-list@python.org> |
| In reply to | #111159 |
On 2016-07-06 01:05, Seymore4Head wrote:
> On Wed, 6 Jul 2016 01:05:12 +0100, MRAB <python@mrabarnett.plus.com>
> wrote:
>
>>On 2016-07-06 00:45, Seymore4Head wrote:
>>> On Tue, 05 Jul 2016 19:29:21 -0400, Seymore4Head
>>> <Seymore4Head@Hotmail.invalid> wrote:
>>>
>>>>On Tue, 5 Jul 2016 19:15:23 -0400, Joel Goldstick
>>>><joel.goldstick@gmail.com> wrote:
>>>>
>>>>>On Tue, Jul 5, 2016 at 7:03 PM, MRAB <python@mrabarnett.plus.com> wrote:
>>>>>> On 2016-07-05 23:05, Seymore4Head wrote:
>>>>>>>
>>>>>>> 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')
>>>>>>>
>>>>>>>
>>>>>>> I just tried to reuse this program that was posted several months ago.
>>>>>>> I am using a text flie that is about 200 lines long and have named it
>>>>>>> win.txt. The file it creates when I run the program is win_new.txt
>>>>>>> but it's empty.
>>>>>>>
>>>>>> Although it creates a file called "win_new.txt", it then renames it to
>>>>>> "win.txt", so "win_new.txt" shouldn't exist.
>>>>>>
>>>>>> Of course, if there's already a file called "win_old.txt", then the first
>>>>>> rename will raise an exception, and you'll have "win_new.txt" and the
>>>>>> original "win.txt".
>>>>>>
>>>>>> --
>>>>>> https://mail.python.org/mailman/listinfo/python-list
>>>>>
>>>>>Why don't you comment out the renames, and see what happens?
>>>>
>>>>I really don't care if the filename gets renamed or not. I commented
>>>>out the renames, but I still get a new file called win_new.txt and it
>>>>is empty.
>>>>
>>>>The original is unchanged.
>>>
>>> I just tried this on a 3 line text file and it works.
>>>
>>> I am looking through the text file and have found at least two
>>> suspicious characters. One is a German letter and the other is a
>>> characters that has been replaced by a square symbol.
>>>
>>That suggests to me that it's an encoding problem (the traceback
>>would've indicated that).
>>
>>Specify an encoding when you open the files:
>>
>>f_in = open('win.txt', 'r', encoding='utf-8')
>>f_out = open('win_new.txt', 'w', encoding='utf-8')
>>
>>assuming that 'win.txt' is indeed encoded in UTF-8. (It might be
>>something like ISO-8859-1 instead.)
>
> Thanks.
>
> It is working now that I removed those two characters.
>
So, you didn't really fix it, you just changed the input to what your
program is able to handle... :-)
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2016-07-06 12:35 +1000 |
| Message-ID | <577c6e56$0$1586$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #111159 |
On Wed, 6 Jul 2016 10:05 am, Seymore4Head wrote:
> On Wed, 6 Jul 2016 01:05:12 +0100, MRAB <python@mrabarnett.plus.com>
> wrote:
>>That suggests to me that it's an encoding problem (the traceback
>>would've indicated that).
>>
>>Specify an encoding when you open the files:
>>
>>f_in = open('win.txt', 'r', encoding='utf-8')
>>f_out = open('win_new.txt', 'w', encoding='utf-8')
>>
>>assuming that 'win.txt' is indeed encoded in UTF-8. (It might be
>>something like ISO-8859-1 instead.)
>
> Thanks.
>
> It is working now that I removed those two characters.
And it will break again next time you use the script, if the input file
contains "those characters" (which, I stress, are perfectly reasonable
characters for a text file to contain).
What you should take from this is Chris' advice to learn better debugging
skills: I am absolutely certain that Python will have displayed a traceback
hinting what went wrong, which you could then use to solve the problem for
good rather than just cover it up. If you're not seeing that traceback,
your ability to program is crippled. You'll be like somebody trying to
assemble a jigsaw puzzle in the dark, wearing thick mittens.
So you're first priority ought to be to work out how to run Python code
without the tracebacks disappearing.
(Either that, or make sure your code is perfect, first time, every time.)
At the moment, you're in the situation of a car manufacturer that has just
discovered a defect in their cars:
"If you drive through a puddle with the rear left wheel at high speed, water
splashes up into the body, causes a short in the electronics, and the gas
tank catches fire."
"That's okay, we can just pave over the factory grounds and get rid of the
puddles."
Your script "catches fire" when given input containing non-ASCII characters.
You don't really know why, and you haven't fixed it, but you've "solved"
the problem by "paving over the puddles". Except not really -- the problem
is still there, and if your script ever gets used on a puddle you've
missed, it will catch fire again.
--
Steven
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.
[toc] | [prev] | [next] | [standalone]
| From | Larry Hudson <orgnut@yahoo.com> |
|---|---|
| Date | 2016-07-05 21:37 -0700 |
| Message-ID | <o-6dnQHLDeVoF-HKnZ2dnUU7-YPNnZ2d@giganews.com> |
| In reply to | #111144 |
On 07/05/2016 03:05 PM, Seymore4Head wrote:
> 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')
>
>
> I just tried to reuse this program that was posted several months ago.
> I am using a text flie that is about 200 lines long and have named it
> win.txt. The file it creates when I run the program is win_new.txt
> but it's empty.
>
>
Not your problem, but you can simplify your read/write loop to:
for line in f_in:
f_out.write(line[:-1] + ' *\n')
The 'line[:-1]' expression gives you the line up to but not including the trailing newline.
Alternately, use: f_out.write(line.rstrip() + ' *\n')
--
-=- Larry -=-
[toc] | [prev] | [next] | [standalone]
| From | cs@zip.com.au |
|---|---|
| Date | 2016-07-06 15:35 +1000 |
| Message-ID | <mailman.119.1467784794.2295.python-list@python.org> |
| In reply to | #111169 |
On 05Jul2016 21:37, Python List <python-list@python.org> wrote:
>On 07/05/2016 03:05 PM, Seymore4Head wrote:
>>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')
>>
>>I just tried to reuse this program that was posted several months ago.
>>I am using a text flie that is about 200 lines long and have named it
>>win.txt. The file it creates when I run the program is win_new.txt
>>but it's empty.
Put a counter in your loop:
count = 0
for line in f_in.read().splitlines():
f_out.write(line + " *\n")
count += 1
print("count =", count)
Check that it says 200 (or whatever number you expect).
>Not your problem, but you can simplify your read/write loop to:
>
>for line in f_in:
> f_out.write(line[:-1] + ' *\n')
>
>The 'line[:-1]' expression gives you the line up to but not including the trailing newline.
>Alternately, use: f_out.write(line.rstrip() + ' *\n')
Importantly for this version, every line _MUST_ have a trailing newline.
Personally that is what I require of my text files anyway, but some dubious
tools (and, IMO, dubious people) make text files with no final newline.
For such a file the above code would eat the last character because we don't
check that a newline is there.
I take a hard line on such files and usually write programs that look like
this:
for line in f_in:
if not line.endswith('\n'):
raise ValueError("missing final newline on file, last line is: %r" (line,))
f_out.write(line[:-1] + ' *\n')
Then one can proceed secure in the knowledge that the data are well formed.
I consider the final newline something of a termination record; without it I
have no faith that the file wasn't rudely truncated somehow. In other words, I
consider a text file to consist of newline-terminated lines, not
newline-separated lines.
Cheers,
Cameron Simpson <cs@zip.com.au>
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web