473,406 Members | 2,867 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,406 software developers and data experts.

Python Writing Output To Separate Files

Following is the code in python that opens all files under a directories and puts a comma in place of whitespace, I can see the output on the command line, but I am not sure what to do in order to write the output of each file read in the directory into a separate file.
# whitespace by comma in every line

import fileinput, string, sys, os
from os.path import join


if len(sys.argv) != 2:
print "Enter: python %s input_directory" % os.path.basename(sys.argv[0])
sys.exit(0)

#Getting input from the command line
directory= sys.argv[1]

for root, dirs, files in os.walk(directory):
for name in files:
filename= os.path.join(root,name)
file= open(filename, 'r')

#putting comma wherever there is occurence of whitespace in line
for line in file:
x= line.split()
str = ''
for i in x:
str+=(","+i)
str1= str.lstrip(',')

print( str1)
file.close()

Please make the changes to the above code so that if there r 3 files under the given directory it weill create 3 separate output files with the result stored in them.
Jun 17 '08 #1
1 2536
bvdet
2,851 Expert Mod 2GB
Please use code tags when posting code. The following will open a file, read each line, split each line into a list, rejoin the list with commas, then write the list to disk.
Expand|Select|Wrap|Line Numbers
  1. filename = 'input_file.txt'
  2. outname = 'output_file.txt'
  3.  
  4. f = open(filename)
  5. lineList = [','.join(items) for items in [line.split() for line in f]]
  6. f.close()
  7.  
  8. f = open(outname, 'w')
  9. f.write('\n'.join(lineList))
  10. f.close()
Jun 18 '08 #2

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

Similar topics

10
by: Andrew Dalke | last post by:
Is there an author index for the new version of the Python cookbook? As a contributor I got my comp version delivered today and my ego wanted some gratification. I couldn't find my entries. ...
10
by: TokiDoki | last post by:
Hello there, I have been programming python for a little while, now. But as I am beginning to do more complex stuff, I am running into small organization problems. It is possible that what I...
16
by: Claudio Grondi | last post by:
I have a 250 Gbyte file (occupies the whole hard drive space) and want to change only eight bytes in this file at a given offset of appr. 200 Gbyte (all other data in that file should remain...
12
by: adamurbas | last post by:
ya so im pretty much a newb to this whole python thing... its pretty cool but i just started today and im already having trouble. i started to use a tutorial that i found somewhere and i followed...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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,...

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.