Writing to a file
Littlefield, Tyler
tyler at tysdomain.com
Fri Mar 25 21:39:11 EDT 2011
> with open(test_absname, 'w') as test:
what's the difference in that and test = ...? I can see why you
mentioned the os.path for cross-platform, but I don't understand why
someone would use with over =.
On 3/25/2011 7:11 PM, eryksun () wrote:
> On Friday, March 25, 2011 11:07:19 AM UTC-4, jyou... at kc.rr.com wrote:
>>>>> f = open('~/Desktop/test.txt', 'w')
>>>>> f.write('testing 1... 2... 3...')
>>>>> f.close()
> Consider using "with" to automatically close the file and os.path for cross-platform compatibility:
>
> import os.path
> user_home = os.path.expanduser('~')
> test_absname = os.path.join(user_home, 'Desktop', 'test.txt')
>
> with open(test_absname, 'w') as test:
> test.write('testing 1... 2... 3...')
>
--
Thanks,
Ty
More information about the Python-list
mailing list