Re: [Tutor] Hello World in Python without space
On Saturday 16 July 2011 03:15:12 Richard D. Moores wrote: 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? Up to now, knowing no better ;-), I have opened the file in, or copied and pasted the contents of a file into, a word processor and turned on the non-printing characters. It is then easy to see extraneous spaces, empty lines etc. I then go back to the editor and do the revealed editing. Rough, ready and cobbled - but easy, and it works. ;-) If you use Windows and have no idea what I am talking about, I apologise. It is so long since I used Windows that I have forgotten much of what it can and cannot do; and I don't know whether it can do this. Lisi ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hello World in Python without space
On Sun, Jul 10, 2011 at 05:05, Peter Otten __pete...@web.de wrote: help(print) shows print(...) print(value, ..., sep=' ', end='\n', file=sys.stdout) Prints the values to a stream, or to sys.stdout by default. Optional keyword arguments: file: a file-like object (stream); defaults to the current sys.stdout. sep: string inserted between values, default a space. end: string appended after the last value, default a newline. I didn't know that printing to a file with print() was possible, so I tried print(Hello, world!, file=C:\test\test.txt) Traceback (most recent call last): File string, line 1, in fragment builtins.AttributeError: 'str' object has no attribute 'write' And the docs at http://docs.python.org/py3k/library/functions.html#print tell me The file argument must be an object with a write(string) method; if it is not present or None, sys.stdout will be used. What do I do to test.txt to make it an object with a write(string) method? Thanks, Dick Moores ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hello World in Python without space
Richard D. Moores, 15.07.2011 23:21: On Sun, Jul 10, 2011 at 05:05, Peter Otten wrote: help(print) shows print(...) print(value, ..., sep=' ', end='\n', file=sys.stdout) Prints the values to a stream, or to sys.stdout by default. Optional keyword arguments: file: a file-like object (stream); defaults to the current sys.stdout. sep: string inserted between values, default a space. end: string appended after the last value, default a newline. I didn't know that printing to a file with print() was possible, so I tried print(Hello, world!, file=C:\test\test.txt) Traceback (most recent call last): File string, line 1, infragment builtins.AttributeError: 'str' object has no attribute 'write' And the docs at http://docs.python.org/py3k/library/functions.html#print tell me The file argument must be an object with a write(string) method; if it is not present or None, sys.stdout will be used. What do I do to test.txt to make it an object with a write(string) method? Oh, there are countless ways to do that, e.g. class Writable(object): def __init__(self, something): print(Found a %s % something)) def write(self, s): print(s) print(Hello, world!, file=Writable(C:\\test\\test.txt)) However, I'm fairly sure what you want is this: with open(C:\\test\\test.txt, w) as file_object: print(Hello, world!, file=file_object) Look up open() (open a file) and the with statement (used here basically as a safe way to make sure the file is closed after writing). Also note that \t refers to a TAB character in Python, you used this twice in your file path string. Stefan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hello World in Python without space
On Fri, Jul 15, 2011 at 14:47, Stefan Behnel stefan...@behnel.de wrote: Richard D. Moores, 15.07.2011 23:21: What do I do to test.txt to make it an object with a write(string) method? Oh, there are countless ways to do that, e.g. class Writable(object): def __init__(self, something): print(Found a %s % something)) def write(self, s): print(s) print(Hello, world!, file=Writable(C:\\test\\test.txt)) However, I'm fairly sure what you want is this: with open(C:\\test\\test.txt, w) as file_object: print(Hello, world!, file=file_object) Yes, went with with open(C:\\test\\test.txt, a+) as file_object: print(Hello, world!, file=file_object) Look up open() (open a file) and the with statement (used here basically as a safe way to make sure the file is closed after writing). Also note that \t refers to a TAB character in Python, you used this twice in your file path string. Oops. I'd forgotten about that. Thanks very much, Stefan and Donald. Dick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hello World in Python without space
On Friday 2011 July 15 15:58, Richard D. Moores wrote: On Fri, Jul 15, 2011 at 14:47, Stefan Behnel stefan...@behnel.de wrote: Richard D. Moores, 15.07.2011 23:21: What do I do to test.txt to make it an object with a write(string) method? Oh, there are countless ways to do that, e.g. class Writable(object): def __init__(self, something): print(Found a %s % something)) def write(self, s): print(s) print(Hello, world!, file=Writable(C:\\test\\test.txt)) However, I'm fairly sure what you want is this: with open(C:\\test\\test.txt, w) as file_object: print(Hello, world!, file=file_object) Yes, went with with open(C:\\test\\test.txt, a+) as file_object: print(Hello, world!, file=file_object) Look up open() (open a file) and the with statement (used here basically as a safe way to make sure the file is closed after writing). Also note that \t refers to a TAB character in Python, you used this twice in your file path string. I believe on Windows, you can almost always use a forward slash in a path: C:/somewhere/somewhereelse/ -- I have seen the future and I am not in it. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hello World in Python without space
On Fri, Jul 15, 2011 at 16:21, xDog Walker thud...@gmail.com wrote: I believe on Windows, you can almost always use a forward slash in a path: C:/somewhere/somewhereelse/ 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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hello World in Python without space
On 07/15/2011 07:39 PM, Richard D. Moores wrote: On Fri, Jul 15, 2011 at 16:21, xDog Walkerthud...@gmail.com wrote: I believe on Windows, you can almost always use a forward slash in a path: C:/somewhere/somewhereelse/ 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. DaveA -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hello World in Python without space
On Fri, Jul 15, 2011 at 17:16, Dave Angel d...@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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hello World in Python without space
Richard D. Moores wrote: 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? Opening a file for writing will flush the contents. open(filename, 'w') will do it, taking advantage of Python's garbage collector to (eventually) close the file. The more careful way is hardly any harder: open(filename, 'w').close() and this ensures that the file isn't left open any longer than necessary. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hello World in Python without space
On Fri, Jul 15, 2011 at 21:38, Steven D'Aprano st...@pearwood.info wrote: Richard D. Moores wrote: 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? Opening a file for writing will flush the contents. open(filename, 'w') will do it, taking advantage of Python's garbage collector to (eventually) close the file. The more careful way is hardly any harder: open(filename, 'w').close() and this ensures that the file isn't left open any longer than necessary. open(C:/test/test.txt, 'w').close() Good to know. Thanks, Steven. Dick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hello World in Python without space
On 7/10/2011 4:12 AM Robert H said... Dear all, I have Python 3.2 installed on Windows 7. I am a complete beginner playing around with the basic functions. My problem is the following script: name=world print(Hello, name,!) print(Hello, name+!) Alan mentioned using concatenation as well and .join() is generally preferred, particularly when many strings are involved. Emile ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Hello World in Python without space
Dear all, I have Python 3.2 installed on Windows 7. I am a complete beginner playing around with the basic functions. My problem is the following script: name=world print(Hello, name,!) The result is: Hello world ! However, I don't want the space before the exclamation mark. I want this: Hello world! I tried to solve the problem with e.g.: print(Hello,name.strip(),!) but the result is the same. Can anyone out there help me? Thank you. Regards, Robert ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hello World in Python without space
Sending args to the print command always puts spaces between them. Try: print(Hello {name}!.format(name=name)) 2011/7/10 Robert H hrober...@hotmail.com Dear all, I have Python 3.2 installed on Windows 7. I am a complete beginner playing around with the basic functions. My problem is the following script: name=world print(Hello, name,!) The result is: Hello world ! However, I don't want the space before the exclamation mark. I want this: Hello world! I tried to solve the problem with e.g.: print(Hello,name.strip(),!) but the result is the same. Can anyone out there help me? Thank you. Regards, Robert ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hello World in Python without space
Robert H wrote: I have Python 3.2 installed on Windows 7. I am a complete beginner playing around with the basic functions. My problem is the following script: name=world print(Hello, name,!) The result is: Hello world ! However, I don't want the space before the exclamation mark. I want this: Hello world! I tried to solve the problem with e.g.: print(Hello,name.strip(),!) but the result is the same. print() by default inserts a space between its arguments. You can avoid that by specifying a separator explicitly with the sep keyword. Let me show it in the interactive interpreter which is generally a good place to experiment with small snippets of code: name = Robert print(Hello , name, !, sep=) # Note the explicit after Hello Hello Robert! Another goodie is that you can easily get useful information about modules, classes, keywords, and functions, e. g. help(print) shows print(...) print(value, ..., sep=' ', end='\n', file=sys.stdout) Prints the values to a stream, or to sys.stdout by default. Optional keyword arguments: file: a file-like object (stream); defaults to the current sys.stdout. sep: string inserted between values, default a space. end: string appended after the last value, default a newline. Use help() without argument to learn more about the interactive help. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hello World in Python without space
Robert H hrober...@hotmail.com wrote name=world print(Hello, name,!) Hello world ! However, I don't want the space before the exclamation mark. I want this: Hello world! Can anyone out there help me? Thank you. I see you've already had two answers, a third is to construct the string before printing it. There are various ways to do that: The simplest: output = Hello + name + ! An alternative which is more efficient for larger numbers of substruings is: output = .join([Hello ,name,!]) # thats an empty string to start The thirs is to use a formatstring, but thats what Izz did in his print call. Whichever method you use you then use print(output) Lots of options. As Peter said, use the prompt to experiment to find which works best for you. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor