473,399 Members | 4,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Copy and Reverse

18
So far, I have:

from string import *
def reverse():
fname = raw_input("Enter a Filename: ")
infile = open(fname, 'r')
data = infile.read()
list(data)
print fname[-1:1]



I need python to read the file (whichever the user wants to open), but print it in reverse and save it. I know I need to treat the file as a string, but I am obviously not doing something right.. Any help would be great.
Mar 12 '07 #1
1 1416
bvdet
2,851 Expert Mod 2GB
So far, I have:

from string import *
def reverse():
fname = raw_input("Enter a Filename: ")
infile = open(fname, 'r')
data = infile.read()
list(data)
print fname[-1:1]



I need python to read the file (whichever the user wants to open), but print it in reverse and save it. I know I need to treat the file as a string, but I am obviously not doing something right.. Any help would be great.
Here's one way of doing it:
Expand|Select|Wrap|Line Numbers
  1. lineList = open('your_file').readlines()
  2. outStr = ''
  3. for line in lineList:
  4.     s = list(line.strip())        # strip newline character and make list of characters
  5.     s.reverse()                    # reverse character list in place
  6.     outStr += '%s\n' % (''.join(s))        # create output string for writing
Mar 12 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: jyoti | last post by:
I have sucessfully able to copy file to a remote mechine in our network by writing some asp code. To make it sucessful, I had to keep destination folder in remote mechine as "Share" for IUSER...
75
by: Massimo | last post by:
I'm planning to develop a .NET application using C#, in order to sell it as a shareware and/or as a full package, so I'll need a good way to protect it against piracy. I know some ways to protect...
4
by: jimjim | last post by:
Hello all, class Base { public: virtual void f(){ } }; void userCode(Base base) { } class Derived : public Base{ int i; };
1
by: Catherine Jo Morgan | last post by:
I get the point of backing up the database I'm working on, but what's a prototype copy? How do I make one and why is it important to work on that rather than on the main database? Or is this more...
3
by: G. Dean Blake | last post by:
In a ASP.NET Visual Studio project I have a webform named FinResults_AR.aspx and its associated vb and resx files. I want to copy and rename to FinResults_GL so that I have an exact duplicate...
17
by: baibaichen | last post by:
i have written some code to verify how to disable slicing copy according C++ Gotchas item 30 the follow is my class hierarchy, and note that B is abstract class!! class B { public: explicit...
11
by: not_a_commie | last post by:
So is it legal to copy stuff out of the .NET framework and mix it into your own code? I assume so, since I could call their function directly if I didn't want to avoid the UI library reference in...
1
by: piercy | last post by:
Ill start by outlining whats in place currently. tblData will display a list of people that can be used based on a set of filters that can be set on a filters above (name age etc.etc.). This tbl is...
9
by: puzzlecracker | last post by:
From my understanding, if you declare any sort of constructors, (excluding copy ctor), the default will not be included by default. Is this correct? class Foo{ public: Foo(int); // no...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
Oralloy
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,...
0
jinu1996
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...
0
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...
0
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,...
0
isladogs
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...

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.