[Tutor] Hello World in Python without space

Richard D. Moores rdmoores at gmail.com
Sat Jul 16 04:15:12 CEST 2011


On Fri, Jul 15, 2011 at 17:16, Dave Angel <d at davea.name> wrote:
> On 07/15/2011 07:39 PM, Richard D. Moores wrote:

>> with open("C:/test/test.txt", "a") as file_object:
>>      print("Hello, world!", file=file_object)
>>
>> Yes, that works for me with Windows Vista. However, if test.txt is
>> empty, it puts in a blank line as line 1; line 2 is "Hello, world!".
>>
>> Dick
>> _
>
> I expect that your extra newline was already in the "empty" file.  It cannot
> have anything to do with using the forward slash for the filename.

I see that you are correct. It seems that selecting all the text in a
text file (in Notepad), then hitting the delete key doesn't guarantee
that the file will be left truly blank. The way that consistently
works for me is to place the cursor in the upper left corner of the
file and hold down the delete key.

Running

with open("C:/test/test.txt", "w") as file_object:
      print(file=file_object)

Also works.

But that makes me wonder if there isn't a simpler way to do it with
Python -- to delete the contents of a file without deleting the file?

Dick


More information about the Tutor mailing list