473,545 Members | 1,288 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

write() vs. writelines()

My question is: why write(''.join(. ..)) works slowly than
writelines(...) ? Here's the code:

import sys
import random

#import psyco
#except ImportError:
def encrypt(key, infile, outfile, bufsize=65536):
random.seed(key )
in_buf = infile.read(buf size)
while in_buf:
outfile.write(' '.join(chr(ord( char) ^ random.randint( 0, 255))
for char in in_buf))
in_buf = infile.read(buf size)
if __name__ == '__main__':
import time
key = int(sys.argv[1])
infile = open(sys.argv[2], 'rb')
outfile = open(sys.argv[3], 'wb')
t = time.time()
encrypt(key, infile, outfile)
print time.time() - t

May 26 '06 #1
3 16577
Gregory Petrosyan wrote:
My question is: why write(''.join(. ..)) works slowly than
writelines(...) ? Here's the code:

the first copies all the substring to a single string large enough to
hold all the data, before handing it over to the file object, while the
second just writes the substrings to the file object.


May 26 '06 #2
Thanks for your reply. I understand this fact, but I wonder why
writelines() works slowly -- I think C code can be optimised to work
faster than Python one. Is it correct that writelines(...) is just a
shorthand for

for ch in ...:

May 26 '06 #3
In <11************ *********@j33g2 000cwa.googlegr oups.com>, Gregory
Petrosyan wrote:
Thanks for your reply. I understand this fact, but I wonder why
writelines() works slowly -- I think C code can be optimised to work
faster than Python one. Is it correct that writelines(...) is just a
shorthand for

for ch in ...:

Depends on `...`. If it's a string then yes because a string is a
sequence of characters. But `writelines()` is ment for a sequence of
strings. It's the counterpart of `readlines()`. Then it's just a
shorthand for::

for line in lines:
file.write(line )

Marc 'BlackJack' Rintsch
May 26 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Amy G | last post by:
I am looking to make this code a little "nicer"... any suggestions??? I want to do a "read+" where I would be able to first read the contents of the file... then either close the file out or write to the file without appending. I want to overwrite the content. vacation_message = "blah blah blah" f_vm=open("/home/%s/.vacation.msg"...
by: nephish | last post by:
Hey there, kinda newbie question here. i know how to read the lines of a txt file. i know how to write a txt file. but how do i overwrite a line value with another value ? i mean, how do go to, say, line 3 of a text file and replace what is written on line 3 with something else?
by: Nico Grubert | last post by:
Hi there, I would like to open an existing file that contains some lines of text in order to append a new line at the end of the content. My first try was: >>> f = open('/tmp/myfile', 'w') #create new file for writing >>> f.writelines('123') #write first line >>> f.close()
by: eric dexter | last post by:
This seems to write everything to the shell but not out to a disk file def get_instr(from_file, to_file, instr_num): instr_num_str = str(instr_num) out_list = infile = open(from_file, 'r') for line in infile: # Internally, for calls the files next() method line_list = line.split()
by: mech point | last post by:
I was able to read the data from file into a two dimensional array (lists) rows= but How to write them back into the file. Thank you, srikanth
by: aiwarrior | last post by:
If file.WriteLines( seq ) accepts a list and it says it writes lines, why does it write the whole list in a single line. Be cause of that the reverse of file.writelines(seq) is not file.readlines(). Are the assumptions i made correct? If yes why is this so? I find a function called writelines not actualy writing the list in lines wierd. ...
by: Paul Lemelle | last post by:
I Am trying to output the os.path.walk to a file, but the writelines method complains.... Below is the code, any helpful suggestions would be appreciated. def visit(arg, dirnames, names): print dirnames
by: Paul Lemelle | last post by:
Jeff, Thanks for your reply. I would like to like the absolute path of a directory. I thought that os.listdir just returns the nam itself in a data list. I noticed that none was being return in my example. Do you think that I have the arguments misplaced? Thanks, Paul
by: Christian Heimes | last post by:
dudeja.rajat@gmail.com wrote: Try %% :) Christian
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.