delete a list in text file well i m having a text file which contain time,user name & id, now if i want to modify the time without affecting the id & user name is it possible?
or how i delete that list & enter a new list in that text file

It would be nice, if you could give us an actual example, so we don't have to guess too wildly!

puru|8346|(2007, 3, 26, 0, 36, 59, 0, 85, 0)|
admin|5194|(2007, 3, 28, 3, 2, 21, 1, 86, 0)|

puru|8346|(2007, 3, 26, 0, 36, 59, 0, 85, 0)|
admin|5194|(2007, 3, 28, 3, 2, 21, 1, 86, 0)|

Ah. Here's a stab:

def change_time(record, new_time):

   name,id,time = record.split('|')
   time = str(new_time)
   record = '|'.join((name,id,time)) + '|'
   return record

Jeff

#!/usr/bin/python
import string
import re
import random
import sys
import time
import fileinput

tn = time.time()
t = str(tn)
ss = "5571"
lv = open("/usr/local/https/data/session1.txt","r")
lvr = lv.readlines()
l_lvr = len(lvr)
#print lvr
l_list = []
for i in range(l_lvr):

test = re.split('\|', lvr)
l_list.append(test)
#print l_list
for u,s,t,sp in l_list:
if s == ss:
so_list = [u, s, t, sp]
u = string.join(so_list)
#print f
s_list = u,s,t,sp
e = so_list[2]
print e
to = s_list[2]
#print st_to
too = float(to)
f = tn - too
if f < 3600:
so_list[2] = str(tn)
sn_list = so_list
print sn_list
g =string.join(sn_list)
print g
for line in fileinput.input('/usr/local/https/data/session1.txt', inplace=1):
line = line.replace(u, g)
sys.stdout.write(line)
sys.exit()

else:
#return 1
print "kK"
sys.exit()
print "no"

file line is not replacing with new one suggest

Okay im lost here...this is joining , how does remove the time field and leave the rest in tact...

Ah. Here's a stab:

def change_time(record, new_time):

   name,id,time = record.split('|')
   time = str(new_time)
   record = '|'.join((name,id,time)) + '|'
   return record

Jeff

First, here is the way I __think__ your code looks when properly formatted.

#!/usr/bin/python
##import string          ##    string is deprecated
import re
import random
import sys
import time
import fileinput

tn = time.time()
t = str(tn)
ss = "5571"
lv = open("/usr/local/https/data/session1.txt","r")
lvr = lv.readlines()
l_lvr = len(lvr)
#print lvr
l_list = []
for i in range(l_lvr):

     test = re.split('\|', lvr[i])
     l_list.append(test)
     #print l_list
     for u,s,t,sp in l_list:
          if s == ss:
               so_list = [u, s, t, sp]
               u = string.join(so_list)
               #print f
               s_list = u,s,t,sp
               e = so_list[2]
               print e
               to = s_list[2]
               #print st_to
               too = float(to)
               f = tn - too
               if f < 3600:
                    so_list[2] = str(tn)
                    sn_list = so_list
                    print sn_listg =string.join(sn_list)
               print g
for line in open('/usr/local/https/data/session1.txt', "r"):
     line = line.replace(u, g)
     sys.stdout.write(line)
sys.exit()

##---  don't know where the else goes
else:
#return 1
print "kK"
sys.exit()
print "no"

As you can see, it takes way too much time to try and fiqure out the code. so you are not getting any responses. Just click the "#" icon above the posting box and put your program between the code statements. The basic format of the program would be

fp = open(file_name, "r")
fp_out = open(file_name+".new", "w")
for rec in fp:
     u,g = function_to_calculate_u_g_for this record()
     ##    split the input rec into name, id, time, etc
     ##    write to ".new" file
     fp_out.write("%s %s %s %s\n" % (name, id, u, g))
fp_out.close()

The data is now in a new file. You now move the new file to the old file's name or whatever you want.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.