473,800 Members | 2,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert list to file object without creating an actual file.

I have written a little program that takes as input a text file,
converts
it to a list with appropriate html coding (making it into a nice
table).
Finally I want to upload this list as a textfile using ftp.

If homeworkhtml contains the list of lines;
e.g. homeworkhtml = ["<table>", "<tr>", "<td>", "test", "</td>" .....

I want to call:
ftp.storlines(" STOR " + filename, homeworkhtml)

which gives me the error
Traceback (most recent call last):
File "./testhw.py", line 67, in ?
ftp.storlines(" STOR " + filename, homeworkhtml)
File "/System/Library/Frameworks/Python.framewor k/Versions/2.3/lib/
python2.3/ftplib.py", line 428, in storlines
AttributeError: 'list' object has no attribute 'readline'

Expected since homeworkhtml is in fact not a file. Is there a way
to convert this list to a file object without first writing it to disc
and
then opening the resulting file?

Best,
Bart
Jan 25 '08 #1
2 3547
On Jan 24, 8:57 pm, Bart Kastermans <bkast...@gmail .comwrote:
I have written a little program that takes as input a text file,
converts
it to a list with appropriate html coding (making it into a nice
table).
Finally I want to upload this list as a textfile using ftp.

If homeworkhtml contains the list of lines;
e.g. homeworkhtml = ["<table>", "<tr>", "<td>", "test", "</td>" .....

I want to call:
ftp.storlines(" STOR " + filename, homeworkhtml)

which gives me the error
Traceback (most recent call last):
File "./testhw.py", line 67, in ?
ftp.storlines(" STOR " + filename, homeworkhtml)
File "/System/Library/Frameworks/Python.framewor k/Versions/2.3/lib/
python2.3/ftplib.py", line 428, in storlines
AttributeError: 'list' object has no attribute 'readline'
Perhaps what you want is StringIO. It lets your pretend a string is a
file so ftplib won't choke. You'll have to convert your list to a
string, though (perhaps with join):
from cStringIO import StringIO
fake_file = StringIO("".joi n(my_list))
>
Expected since homeworkhtml is in fact not a file. Is there a way
to convert this list to a file object without first writing it to disc
and
then opening the resulting file?

Best,
Bart
Jan 25 '08 #2
On Thu, 24 Jan 2008 18:57:58 -0800, Bart Kastermans wrote:
I have written a little program that takes as input a text file,
....
Expected since homeworkhtml is in fact not a file. Is there a way to
convert this list to a file object without first writing it to disc and
then opening the resulting file?
The StringIO module is your friend, together with a couple of basic
Python techniques.

>>alist = ["<table>\n" , " <tr>\n",
.... " <td>", "Nobody expects the Spanish Inquisition!",
.... "</td>\n", " </tr>\n", "</table>\n"]
>>print ''.join(alist) # but strings don't have a readlines method...
<table>
<tr>
<td>Nobody expects the Spanish Inquisition!</td>
</tr>
</table>
>>>
f = StringIO.String IO()
f.writelines( alist)
f.getvalue( )
'<table>\n <tr>\n <td>Nobody expects the Spanish Inquisition!</td>\n
</tr>\n</table>\n'
>>f.seek(0) # don't forget to reset the file pointer!
print f.read() # also has readlines
<table>
<tr>
<td>Nobody expects the Spanish Inquisition!</td>
</tr>
</table>


--
Steven
Jan 25 '08 #3

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

Similar topics

5
12722
by: XML newbie: Urgent pls help! | last post by:
function to convert string to 1 dimensional array of long in VB.Net
5
3793
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public List<RoleData> GetRoles() { return GetRoles(null, false); }
26
2597
by: mark | last post by:
The idea of this is very simle. The site is 800px wide and sits in the middle of the browser window, on either side of the site I want a different background image aligned against it. If I were doing this with table based layout the code would be as follows: <head> <style type="text/css"> #bgleft { background: url(left_half_circle.gif) right top repeat-y;
3
8752
by: mrajanikrishna | last post by:
Hi Friends, I am accepting a number from the user entered in a textbox. I want to assign to a variable in my code and assignt this to that variable. double num1 = (double)txtNum1.text; this produced an error
12
2453
by: Mark S. | last post by:
Hello, The app in question is lives on a Windows 2003 server with .NET 2.0 running IIS 6. The page of the app in question processes 2000 get requests a second during peak loads. The app uses a Static Object. In this object is a generic List<String>. For every page request this list is looped over only reading not writing each value.
3
8117
by: WPeterson | last post by:
Converting PowerPoint to Flash would absolutely be a good choice to distribute your bulky PowerPoint presentations. You can do the whole PowerPoint to Flash conversion manually or with professional applications. First, you'll need to prepare the PowerPoint files. Make sure you are not using any complicated gradients or animations. These will be interpreted poorly when they are brought into Flash. Also, make sure there are no objects that...
14
1888
by: Joe | last post by:
this file is drawn in VB.NET and input output goes by XmlSerializer. Therefore, simple output file looks like: <?xml version="1.0" encoding="utf-8"?> <DrawablePicture xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" BackColor="-1250856"> <DrawableRectangle LineWidth="2" X1="377" Y1="88" X2="323" Y2="130" ForeColor="-16777216" BackColor="-1" /> </DrawablePicture>
1
47492
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click on a link and after a moment or two a file download dialog box pops-up in your web browser and prompts you for some instructions, such as “open” or “save“. I’m going to show you how to do that using a perl script. What You Need Any recent...
0
9690
marktang
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9551
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10253
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10033
tracyyun
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9085
agi2029
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5471
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3764
muto222
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.