473,406 Members | 2,698 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.

add a line to a file and then automatically rename it

1
Hi,
What if I want to add a line to a file and then automatically rename it in a cycle using a sequence of numbers. I mean, if the original file has the name file0.txt then the next file should be file1.txt and the next should be file2.txt and so on.

I hope you can give me some help here.

Thanks
Jun 14 '07 #1
2 1479
ghostdog74
511 Expert 256MB
Hi,
What if I want to add a line to a file and then automatically rename it in a cycle using a sequence of numbers. I mean, if the original file has the name file0.txt then the next file should be file1.txt and the next should be file2.txt and so on.

I hope you can give me some help here.

Thanks
not tested...
Expand|Select|Wrap|Line Numbers
  1. import os,shutil
  2. if os.path.exists("num"):
  3.      value=open("num").read().strip()
  4. lineToAdd = "new line"
  5. newfile ="file%s.txt" % str(int(num)+1)
  6. shutil.copy("file0.txt",  newfile)
  7. open(newfile,"a").write(line+"\n")
  8.  
Jun 14 '07 #2
bvdet
2,851 Expert Mod 2GB
Hi,
What if I want to add a line to a file and then automatically rename it in a cycle using a sequence of numbers. I mean, if the original file has the name file0.txt then the next file should be file1.txt and the next should be file2.txt and so on.

I hope you can give me some help here.

Thanks
Expand|Select|Wrap|Line Numbers
  1. import os, re
  2.  
  3. patt = r'\d+\Z'
  4.  
  5. # seed file, name must end with one or more digits
  6. fn = r'X:\directory\subdir\name999.txt'
  7.  
  8. for i in range(5):
  9.     fnList = list(os.path.splitext(fn))
  10.  
  11.     num = re.search(patt, fnList[0]).group(0)
  12.     fnList[0] = re.sub(patt, str(int(num)+1), fnList[0])
  13.  
  14.     stringToAdd = '9876543210'
  15.  
  16.     fn1 = ''.join(fnList)
  17.     f = open(fn1, 'w')
  18.     f.write('%s\n%s' % (open(fn).read(), stringToAdd))
  19.     f.close()
  20.     fn = fn1
Jun 14 '07 #3

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

Similar topics

1
by: Koen | last post by:
Hi all, I created a little database to manage my e-books. The program will synchronize a table with the contents of a directory. Works great. Because I keep additional info (like keywords) to...
1
by: Koen | last post by:
Hi all, I created a little database to manage my e-books. The program will synchronize a table with the contents of a directory. Works great. Because I keep additional info (like keywords) to...
13
by: programming | last post by:
how do i delete from a text file 1 of the following lines: jon|scott adam|smith <--delete paul|clark say i would like to delete the middle line of this txt, in member.txt what php code or...
12
by: dbuchanan | last post by:
Hello, (Is this the proper newsgroup?) === Background === I am building a solution with two projects. One project is my data access layer which contains my DataSet as an xsd file. The XSD...
1
by: hansoncry | last post by:
Hi everyone, i met some difficulties handling the following problem. Say I have a input file in following format: File Name File Number Size(blocks) Size(bytes) file1 1 ...
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?
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
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,...
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
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.