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
2 1479
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... -
import os,shutil
-
if os.path.exists("num"):
-
value=open("num").read().strip()
-
lineToAdd = "new line"
-
newfile ="file%s.txt" % str(int(num)+1)
-
shutil.copy("file0.txt", newfile)
-
open(newfile,"a").write(line+"\n")
-
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
- import os, re
-
-
patt = r'\d+\Z'
-
-
# seed file, name must end with one or more digits
-
fn = r'X:\directory\subdir\name999.txt'
-
-
for i in range(5):
-
fnList = list(os.path.splitext(fn))
-
-
num = re.search(patt, fnList[0]).group(0)
-
fnList[0] = re.sub(patt, str(int(num)+1), fnList[0])
-
-
stringToAdd = '9876543210'
-
-
fn1 = ''.join(fnList)
-
f = open(fn1, 'w')
-
f.write('%s\n%s' % (open(fn).read(), stringToAdd))
-
f.close()
-
fn = fn1
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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 ...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
|
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: 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...
| |