ST_CTIME convert to yyyymmdd
Hi, Any hint on converting time from ST_CTIME secs into mmdd format? sorry for one-liner stupid question.. I couldn;t find (or rather figure out) in docs. Thank you, hj -- http://mail.python.org/mailman/listinfo/python-list
Re: ST_CTIME convert to yyyymmdd
All right I got it. Thank you anyway... create_date = os.stat(pathname)[ST_CTIME] print time.strftime("%Y%m%d", time.gmtime(create_date)) Hitesh wrote: > Hi, > > Any hint on converting time from ST_CTIME secs into mmdd format? > sorry for one-liner stupid question.. I couldn;t find (or rather figure > out) in docs. > > Thank you, > hj -- http://mail.python.org/mailman/listinfo/python-list
Getting previous file name
Hi, I have a small script here that goes to inside dir and sorts the file by create date. I can return the create date but I don't know how to find the name of that file... I need file that is not latest but was created before the last file. Any hints... I am newbiw python dude and still trying to figure out lot of 'stuff'.. import os, time, sys from stat import * def walktree(path): test1 = [] for f in os.listdir(path): filename = os.path.join(path, f) create_date_sces = os.stat(filename)[ST_CTIME] create_date = time.strftime("%Y%m%d%H%M%S", time.localtime(create_date_sces)) print create_date, " ." , f test1.append(create_date) test1.sort() print test1 return test1[-2] if __name__ == '__main__': path = 'srv12\\c$\\backup\\my_folder\\' prev_file = walktree(path) print "Previous back file is ", prev_file Thank you, hj -- http://mail.python.org/mailman/listinfo/python-list
Re: Getting previous file name
Thank you everyone. It worked. Here is the BETA 0.9 :) import os, time, sys from stat import * def findfile(path): file_list = [] for f in os.listdir(path): filename = os.path.join(path, f) if not os.path.isfile(filename): print "*** Not a file:", repr(filename) continue create_date_secs = os.stat(filename)[ST_CTIME] create_date = time.strftime("%Y%m%d%H%M%S", time.localtime(create_date_secs)) file_list.append((create_date, filename)) file_list.sort() print file_list[-2] return file_list[-2] if __name__ == '__main__': path = r'\\rad-db02-ny\c$\backup\rad_oltp' create_date, prev_file = findfile(path) print "Previous back file is: ", prev_file, " ", create_date Now I am going to read this file and manupulate stuff with DB so I am going to work on ODBC connection using python.. interesting stuff. Thank you all, hj Hitesh wrote: > Hi, > > I have a small script here that goes to inside dir and sorts the file > by create date. I can return the create date but I don't know how to > find the name of that file... > I need file that is not latest but was created before the last file. > Any hints... I am newbiw python dude and still trying to figure out lot > of 'stuff'.. > > > import os, time, sys > from stat import * > > def walktree(path): > test1 = [] > for f in os.listdir(path): > filename = os.path.join(path, f) > create_date_sces = os.stat(filename)[ST_CTIME] > create_date = time.strftime("%Y%m%d%H%M%S", > time.localtime(create_date_sces)) > print create_date, " ." , f > test1.append(create_date) > test1.sort() > print test1 > return test1[-2] > > > if __name__ == '__main__': > path = 'srv12\\c$\\backup\\my_folder\\' > prev_file = walktree(path) > print "Previous back file is ", prev_file > > > Thank you, > hj -- http://mail.python.org/mailman/listinfo/python-list
Re: Getting previous file name
John Machin wrote: > Hitesh wrote: > > Hi, > > > > I have a small script here that goes to inside dir and sorts the file > > by create date. I can return the create date but I don't know how to > > find the name of that file... > > I need file that is not latest but was created before the last file. > > Any hints... I am newbiw python dude and still trying to figure out lot > > of 'stuff'.. > > > > > > import os, time, sys > > from stat import * > > Lose that, and use ".st_ctime" instead of "[ST_CTIME]" below > Not sure how to do that so I am going to leave it alone. > > > > def walktree(path): > > This function name is rather misleading. The function examines only the > entries in the nominated path. If any of those entries are directories, > it doesn't examine their contents. > > > test1 = [] > > for f in os.listdir(path): > > filename = os.path.join(path, f) > > os.listdir() gives you directories etc as well as files. Import > os.path, and add something like this: > > if not os.path.isfile(filename): > print "*** Not a file:", repr(filename) > continue > This is cool stuff. I am stuffing this inside my script. > > create_date_sces = os.stat(filename)[ST_CTIME] > > Do you mean "secs" rather than "sces"? Yes I mean secs not sces. > > > create_date = time.strftime("%Y%m%d%H%M%S", > > time.localtime(create_date_sces)) > > print create_date, " ." , f > > test1.append(create_date) > > Answer to your main question: change that to > test1.append((create_date, filename)) > and see what happens. > > > test1.sort() > > If there is any chance that multiple files can be created inside 1 > second, you have a problem -- even turning on float results by using > os.stat_float_times(True) (and changing "[ST_CTIME]" to ".st_ctime") > doesn't help; the Windows result appears to be no finer than 1 second > granularity. The pywin32 package may provide a solution. > > > print test1 > > return test1[-2] > > > > > > if __name__ == '__main__': > > path = 'srv12\\c$\\backup\\my_folder\\' > > (1) Use raw strings. (2) You don't need the '\' on the end. > E.g. > path = r'\\srv12\c$\backup\my_folder' > > > prev_file = walktree(path) > > print "Previous back file is ", prev_file > > Thank you hj -- http://mail.python.org/mailman/listinfo/python-list
Re: Getting previous file name
Thank you all. Here is my BETA ver. import os, time, sys from stat import * def findfile(path): file_list = [] for f in os.listdir(path): filename = os.path.join(path, f) if not os.path.isfile(filename): print "*** Not a file:", repr(filename) continue create_date_secs = os.stat(filename)[ST_CTIME] create_date = time.strftime("%Y%m%d%H%M%S", time.localtime(create_date_secs)) #print create_date, " ." , f file_list.append((create_date, filename)) file_list.sort() print file_list[-2] return file_list[-2] if __name__ == '__main__': path = r'srv12\\c$\\backup\\my_folder' create_date, prev_file = findfile(path) Thank you hj Hitesh wrote: > Hi, > > I have a small script here that goes to inside dir and sorts the file > by create date. I can return the create date but I don't know how to > find the name of that file... > I need file that is not latest but was created before the last file. > Any hints... I am newbiw python dude and still trying to figure out lot > of 'stuff'.. > > > import os, time, sys > from stat import * > > def walktree(path): > test1 = [] > for f in os.listdir(path): > filename = os.path.join(path, f) > create_date_sces = os.stat(filename)[ST_CTIME] > create_date = time.strftime("%Y%m%d%H%M%S", > time.localtime(create_date_sces)) > print create_date, " ." , f > test1.append(create_date) > test1.sort() > print test1 > return test1[-2] > > > if __name__ == '__main__': > path = 'srv12\\c$\\backup\\my_folder\\' > prev_file = walktree(path) > print "Previous back file is ", prev_file > > > Thank you, > hj -- http://mail.python.org/mailman/listinfo/python-list
Adding a char inside path string
Hi, I get path strings from a DB like: \\serverName\C:\FolderName1\FolderName2\example.exe I am writing a script that can give me access to that exe file. But problem is that string is not universal path, I need to add C$. Any idea how I can add $ char in that string. ServerName is not fixed length. It could be any chars length. Thank you, hj -- http://mail.python.org/mailman/listinfo/python-list
Re: Adding a char inside path string
Thank you Fredrik. That works for a string. But I am getting list of tuples from DB. rows = [('\\serverName\C:\FolderName1\FolderName2\example.exe',), ('\\serverName\C:\FolderName1\FolderName2\example2.exe',), ('\\serverName\C:\FolderName1\FolderName2\example3.exe',), ('\\serverName\C:\FolderName1\FolderName2\example4.exe',)] I tried this: for i in rows: row = str(i) path = row.replace("C:" , "c$") print path I am getting path something like ('\\serverName\c$:\FolderName1\FolderName2\example.exe',) How on the earth I can remove those paranthesis? ty hj Fredrik Lundh wrote: > "Hitesh" wrote: > > > I get path strings from a DB like: > > > > \\serverName\C:\FolderName1\FolderName2\example.exe > > > > I am writing a script that can give me access to that exe file. > > But problem is that string is not universal path, I need to add C$. > > Any idea how I can add $ char in that string. > > ServerName is not fixed length. It could be any chars length. > > upath = path.replace("C:", "C$") > > -- http://mail.python.org/mailman/listinfo/python-list
Re: Adding a char inside path string
Thank you all it worked!. Tim, > modRows = ['\\'+itm[0].replace(":", "$") for itm in rows] What are those two forward slashes for? I had to remove them otherwise I was getting output like '\\' inside list or if I print I was getting like \\\ Thanks, hj Tim Williams wrote: > On 16/08/06, Dennis Lee Bieber <[EMAIL PROTECTED]> wrote: > > On 16 Aug 2006 09:00:57 -0700, "Hitesh" <[EMAIL PROTECTED]> declaimed > > the following in comp.lang.python: > > > > > > > > Thank you Fredrik. That works for a string. > > > But I am getting list of tuples from DB. > > > > > > rows = [('\\serverName\C:\FolderName1\FolderName2\example.exe',), > > > ('\\serverName\C:\FolderName1\FolderName2\example2.exe',), > > > ('\\serverName\C:\FolderName1\FolderName2\example3.exe',), > > > ('\\serverName\C:\FolderName1\FolderName2\example4.exe',)] > > > > > > I tried this: > > > for i in rows: > > > row = str(i) > > > path = row.replace("C:" , "c$") > > > print path > > > > > > I am getting path something like > > > > > > ('\\serverName\c$:\FolderName1\FolderName2\example.exe',) > > > > > > How on the earth I can remove those paranthesis? > > > > > By accessing the contents of the tuple, not the tuple itself > > > > >>> rows = [('\\serverName\C:\FolderName1\FolderName2\example.exe',), > > ('\\serverName\C:\FolderName1\FolderName2\example2.exe',), > > ('\\serverName\C:\FolderName1\FolderName2\example3.exe',), > > ('\\serverName\C:\FolderName1\FolderName2\example4.exe',)] > > >>> rows > > [('\\serverName\\C:\\FolderName1\\FolderName2\\example.exe',), > > ('\\serverName\\C:\\FolderName1\\FolderName2\\example2.exe',), > > ('\\serverName\\C:\\FolderName1\\FolderName2\\example3.exe',), > > ('\\serverName\\C:\\FolderName1\\FolderName2\\example4.exe',)] > > >>> modRows = [itm[0].replace("C:", "C$") for itm in rows] > > >>> modRows > > ['\\serverName\\C$\\FolderName1\\FolderName2\\example.exe', > > '\\serverName\\C$\\FolderName1\\FolderName2\\example2.exe', > > '\\serverName\\C$\\FolderName1\\FolderName2\\example3.exe', > > '\\serverName\\C$\\FolderName1\\FolderName2\\example4.exe'] > > >>> > > Try > > modRows = ['\\'+itm[0].replace(":", "$") for itm in rows] > > It will work with any drive letter and makes an allowance for the > first escape character. > > >>> modRows > ['serverName\\C$\\FolderName1\\FolderName2\\example.exe', > 'serverName\\C$\\FolderName1\\FolderName2\\example2.exe', > etc > > for r in modRows: > print r > > \\serverName\C$\FolderName1\FolderName2\example.exe > \\serverName\C$\FolderName1\FolderName2\example2.exe > ..etc > > > :) -- http://mail.python.org/mailman/listinfo/python-list
Documentation Question About Depricated String Functions
Hi, In python doc -- 4.1.4 Deprecated string functions -- I read that "The following list of functions are also defined as methods of string and Unicode objects; see ``String Methods'' (section 2.3.6) for more information on those. You should consider these functions as deprecated, although they will not be removed until Python 3.0. The functions defined in this module are: " and there is a whole list of functions. If these functions are deprecated what is the replacement for them? I couldn't find that info in the doc. Any links will be helpful. Thank you, hj -- http://mail.python.org/mailman/listinfo/python-list
Re: Documentation Question About Depricated String Functions
Thank you guys. hj Duncan Booth wrote: > Hitesh wrote: > > > In python doc -- 4.1.4 Deprecated string functions -- I read that "The > > following list of functions are also defined as methods of string and > > Unicode objects; see ``String Methods'' (section 2.3.6) for more > > information on those. You should consider these functions as > > deprecated, although they will not be removed until Python 3.0. The > > functions defined in this module are: " and there is a whole list of > > functions. If these functions are deprecated what is the replacement > > for them? I couldn't find that info in the doc. Any links will be > > helpful. > > You did find the info in the doc - you quoted it above: > > The functions are deprecated: use the string methods (see section 2.3.6) > instead. -- http://mail.python.org/mailman/listinfo/python-list
Re: Adding a char inside path string
Hi, Everything is working fine and dandy but I ran across another issue here. Some of the path comes with some extra chars padded at the end. i.e. '\\serverName\C:\FolderName1\FolderName2\example.exe' -u ABC -g XYZ abcdef Now those padded chars are not constant all the time. It can be anything. Only common denometer in each string that comes with those padded chars is that it is after .exe and then there is space and then 99% of the time it is -u and then there can be anything, I meant its dynemic after that. so I am using string1.find(".exe") and could retrive the index but don't know how to get rid any garbase after index + 4 hj Dennis Lee Bieber wrote: > On 16 Aug 2006 09:00:57 -0700, "Hitesh" <[EMAIL PROTECTED]> declaimed > the following in comp.lang.python: > > > > > Thank you Fredrik. That works for a string. > > But I am getting list of tuples from DB. > > > > rows = [('\\serverName\C:\FolderName1\FolderName2\example.exe',), > > ('\\serverName\C:\FolderName1\FolderName2\example2.exe',), > > ('\\serverName\C:\FolderName1\FolderName2\example3.exe',), > > ('\\serverName\C:\FolderName1\FolderName2\example4.exe',)] > > > > I tried this: > > for i in rows: > > row = str(i) > > path = row.replace("C:" , "c$") > > print path > > > > I am getting path something like > > > > ('\\serverName\c$:\FolderName1\FolderName2\example.exe',) > > > > How on the earth I can remove those paranthesis? > > > By accessing the contents of the tuple, not the tuple itself > > >>> rows = [('\\serverName\C:\FolderName1\FolderName2\example.exe',), > ... ('\\serverName\C:\FolderName1\FolderName2\example2.exe',), > ... ('\\serverName\C:\FolderName1\FolderName2\example3.exe',), > ... ('\\serverName\C:\FolderName1\FolderName2\example4.exe',)] > >>> rows > [('\\serverName\\C:\\FolderName1\\FolderName2\\example.exe',), > ('\\serverName\\C:\\FolderName1\\FolderName2\\example2.exe',), > ('\\serverName\\C:\\FolderName1\\FolderName2\\example3.exe',), > ('\\serverName\\C:\\FolderName1\\FolderName2\\example4.exe',)] > >>> modRows = [itm[0].replace("C:", "C$") for itm in rows] > >>> modRows > ['\\serverName\\C$\\FolderName1\\FolderName2\\example.exe', > '\\serverName\\C$\\FolderName1\\FolderName2\\example2.exe', > '\\serverName\\C$\\FolderName1\\FolderName2\\example3.exe', > '\\serverName\\C$\\FolderName1\\FolderName2\\example4.exe'] > >>> > -- > WulfraedDennis Lee Bieber KD6MOG > [EMAIL PROTECTED] [EMAIL PROTECTED] > HTTP://wlfraed.home.netcom.com/ > (Bestiaria Support Staff: [EMAIL PROTECTED]) > HTTP://www.bestiaria.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Adding a char inside path string
That might work but what if I get (in future) a path name where foldername (and in windows it is very common, someone might name a folder something like "Screw You") with space? Larry Bates wrote: > Sounds like you can split the string on a space and throw > away the right side: > > s='\\serverName\C:\FolderName1\FolderName2\example.exe' -u ABC -g XYZ > p=s.split(" ", 1)[0] > print p > > '\\serverName\C:\FolderName1\FolderName2\example.exe' > > Larry Bates > > Hitesh wrote: > > > > > > Hi, > > > > Everything is working fine and dandy but I ran across another issue > > here. > > Some of the path comes with some extra chars padded at the end. > > i.e. > > > > '\\serverName\C:\FolderName1\FolderName2\example.exe' -u ABC -g XYZ > > abcdef > > > > Now those padded chars are not constant all the time. It can be > > anything. > > Only common denometer in each string that comes with those padded chars > > is that it is after .exe and then there is space and then 99% of the > > time it is -u and then there can be anything, I meant its dynemic after > > that. > > > > so I am using string1.find(".exe") and could retrive the index but > > don't know how to get rid any garbase after index + 4 > > > > hj > > > > > > Dennis Lee Bieber wrote: > >> On 16 Aug 2006 09:00:57 -0700, "Hitesh" <[EMAIL PROTECTED]> declaimed > >> the following in comp.lang.python: > >> > >>> Thank you Fredrik. That works for a string. > >>> But I am getting list of tuples from DB. > >>> > >>> rows = [('\\serverName\C:\FolderName1\FolderName2\example.exe',), > >>> ('\\serverName\C:\FolderName1\FolderName2\example2.exe',), > >>> ('\\serverName\C:\FolderName1\FolderName2\example3.exe',), > >>> ('\\serverName\C:\FolderName1\FolderName2\example4.exe',)] > >>> > >>> I tried this: > >>> for i in rows: > >>> row = str(i) > >>> path = row.replace("C:" , "c$") > >>> print path > >>> > >>> I am getting path something like > >>> > >>> ('\\serverName\c$:\FolderName1\FolderName2\example.exe',) > >>> > >>> How on the earth I can remove those paranthesis? > >>> > >>By accessing the contents of the tuple, not the tuple itself > >> > >>>>> rows = [('\\serverName\C:\FolderName1\FolderName2\example.exe',), > >> ... ('\\serverName\C:\FolderName1\FolderName2\example2.exe',), > >> ... ('\\serverName\C:\FolderName1\FolderName2\example3.exe',), > >> ... ('\\serverName\C:\FolderName1\FolderName2\example4.exe',)] > >>>>> rows > >> [('\\serverName\\C:\\FolderName1\\FolderName2\\example.exe',), > >> ('\\serverName\\C:\\FolderName1\\FolderName2\\example2.exe',), > >> ('\\serverName\\C:\\FolderName1\\FolderName2\\example3.exe',), > >> ('\\serverName\\C:\\FolderName1\\FolderName2\\example4.exe',)] > >>>>> modRows = [itm[0].replace("C:", "C$") for itm in rows] > >>>>> modRows > >> ['\\serverName\\C$\\FolderName1\\FolderName2\\example.exe', > >> '\\serverName\\C$\\FolderName1\\FolderName2\\example2.exe', > >> '\\serverName\\C$\\FolderName1\\FolderName2\\example3.exe', > >> '\\serverName\\C$\\FolderName1\\FolderName2\\example4.exe'] > >> -- > >>WulfraedDennis Lee Bieber KD6MOG > >>[EMAIL PROTECTED] [EMAIL PROTECTED] > >>HTTP://wlfraed.home.netcom.com/ > >>(Bestiaria Support Staff: [EMAIL PROTECTED]) > >>HTTP://www.bestiaria.com/ > > -- http://mail.python.org/mailman/listinfo/python-list
Re: Adding a char inside path string
>>> s = '\\serverName\C:\Folder Name1\FolderName2\example.exe -u ABC -g XYZ' >>> p = s.split(" ", 1)[0] >>> p '\\serverName\\C:\\Folder' hj Larry Bates wrote: > Sounds like you can split the string on a space and throw > away the right side: > > s='\\serverName\C:\FolderName1\FolderName2\example.exe' -u ABC -g XYZ > p=s.split(" ", 1)[0] > print p > > '\\serverName\C:\FolderName1\FolderName2\example.exe' > > Larry Bates > > Hitesh wrote: > > > > > > Hi, > > > > Everything is working fine and dandy but I ran across another issue > > here. > > Some of the path comes with some extra chars padded at the end. > > i.e. > > > > '\\serverName\C:\FolderName1\FolderName2\example.exe' -u ABC -g XYZ > > abcdef > > > > Now those padded chars are not constant all the time. It can be > > anything. > > Only common denometer in each string that comes with those padded chars > > is that it is after .exe and then there is space and then 99% of the > > time it is -u and then there can be anything, I meant its dynemic after > > that. > > > > so I am using string1.find(".exe") and could retrive the index but > > don't know how to get rid any garbase after index + 4 > > > > hj > > > > > > Dennis Lee Bieber wrote: > >> On 16 Aug 2006 09:00:57 -0700, "Hitesh" <[EMAIL PROTECTED]> declaimed > >> the following in comp.lang.python: > >> > >>> Thank you Fredrik. That works for a string. > >>> But I am getting list of tuples from DB. > >>> > >>> rows = [('\\serverName\C:\FolderName1\FolderName2\example.exe',), > >>> ('\\serverName\C:\FolderName1\FolderName2\example2.exe',), > >>> ('\\serverName\C:\FolderName1\FolderName2\example3.exe',), > >>> ('\\serverName\C:\FolderName1\FolderName2\example4.exe',)] > >>> > >>> I tried this: > >>> for i in rows: > >>> row = str(i) > >>> path = row.replace("C:" , "c$") > >>> print path > >>> > >>> I am getting path something like > >>> > >>> ('\\serverName\c$:\FolderName1\FolderName2\example.exe',) > >>> > >>> How on the earth I can remove those paranthesis? > >>> > >>By accessing the contents of the tuple, not the tuple itself > >> > >>>>> rows = [('\\serverName\C:\FolderName1\FolderName2\example.exe',), > >> ... ('\\serverName\C:\FolderName1\FolderName2\example2.exe',), > >> ... ('\\serverName\C:\FolderName1\FolderName2\example3.exe',), > >> ... ('\\serverName\C:\FolderName1\FolderName2\example4.exe',)] > >>>>> rows > >> [('\\serverName\\C:\\FolderName1\\FolderName2\\example.exe',), > >> ('\\serverName\\C:\\FolderName1\\FolderName2\\example2.exe',), > >> ('\\serverName\\C:\\FolderName1\\FolderName2\\example3.exe',), > >> ('\\serverName\\C:\\FolderName1\\FolderName2\\example4.exe',)] > >>>>> modRows = [itm[0].replace("C:", "C$") for itm in rows] > >>>>> modRows > >> ['\\serverName\\C$\\FolderName1\\FolderName2\\example.exe', > >> '\\serverName\\C$\\FolderName1\\FolderName2\\example2.exe', > >> '\\serverName\\C$\\FolderName1\\FolderName2\\example3.exe', > >> '\\serverName\\C$\\FolderName1\\FolderName2\\example4.exe'] > >> -- > >>WulfraedDennis Lee Bieber KD6MOG > >>[EMAIL PROTECTED] [EMAIL PROTECTED] > >>HTTP://wlfraed.home.netcom.com/ > >>(Bestiaria Support Staff: [EMAIL PROTECTED]) > >>HTTP://www.bestiaria.com/ > > -- http://mail.python.org/mailman/listinfo/python-list
Re: Adding a char inside path string
anything after .exe should be truncated (or deleted). Grant Edwards wrote: > On 2006-08-16, Hitesh <[EMAIL PROTECTED]> wrote: > > > That might work but what if I get (in future) a path name where > > foldername (and in windows it is very common, someone might name a > > folder something like "Screw You") with space? > > You must come up with a rigorous specification for what is and > isn't allowed at the end of a path. > > Then delete the stuff that isn't allowed. > > -- > Grant Edwards grante Yow! Hello? Enema > at Bondage? I'm calling >visi.combecause I want to be happy, >I guess... -- http://mail.python.org/mailman/listinfo/python-list
Re: Adding a char inside path string
Here is a mediocare solution. def TruncateString(s, Tindex): return string.ljust(s,Tindex){:Tindex] s = '\\serverName\\C:\\Folder Name1\\FolderName2\\example.exe -u ABC -g XYZ' Sindex = s.find(".exe") Sindex = Tindex +4 s1 = TruncateString(s, Sindex) Hitesh wrote: > anything after .exe should be truncated (or deleted). > > > Grant Edwards wrote: > > On 2006-08-16, Hitesh <[EMAIL PROTECTED]> wrote: > > > > > That might work but what if I get (in future) a path name where > > > foldername (and in windows it is very common, someone might name a > > > folder something like "Screw You") with space? > > > > You must come up with a rigorous specification for what is and > > isn't allowed at the end of a path. > > > > Then delete the stuff that isn't allowed. > > > > -- > > Grant Edwards grante Yow! Hello? Enema > > at Bondage? I'm calling > >visi.combecause I want to be > > happy, > >I guess... -- http://mail.python.org/mailman/listinfo/python-list
Re: Adding a char inside path string
I post a crappy solution but I can add few more stuff to make it fail proof. i.e. I can search for ".exe -u" But if someone names folder like "folder.exe u". This script could fail. Or if in padded garbase I get ".exe u" These are two known issues I have to takcle. Thanks everyone for your help. Grant Edwards wrote: > On 2006-08-16, Hitesh <[EMAIL PROTECTED]> wrote: > > > anything after .exe should be truncated (or deleted). > > That will fail if any directory names contain the string > ".exe", but if that's what you want, it's trivial enough: > > for s in ["asdfasdf.exe -u", "soemthing/else", > "asdf.exe/qwerqwer/qwerqwer.exe"]: > print `s`, > i = s.find(".exe") > print i, > if i >= 0: > s = s[:i+4] > print `s` > > -- > Grant Edwards grante Yow! Yow! It's some people > at inside the wall! This is >visi.combetter than mopping! -- http://mail.python.org/mailman/listinfo/python-list
Re: Adding a char inside path string
How about this: def TruncateString(s, Tindex): return string.ljust(s,Tindex){:Tindex] s = '\\serverName\\C:\\Folder Name1\\FolderName2\\example.exe -u ABC -g XYZ' try: Sindex = s.find(".exe") if Sindex > 0: Sindex = Tindex + 4 s1 = TruncateString(s, Sindex) except: pass hj Hitesh wrote: > I post a crappy solution but I can add few more stuff to make it fail > proof. > i.e. I can search for ".exe -u" > But if someone names folder like "folder.exe u". This script could > fail. > Or if in padded garbase I get ".exe u" > > These are two known issues I have to takcle. > > Thanks everyone for your help. > > > > Grant Edwards wrote: > > On 2006-08-16, Hitesh <[EMAIL PROTECTED]> wrote: > > > > > anything after .exe should be truncated (or deleted). > > > > That will fail if any directory names contain the string > > ".exe", but if that's what you want, it's trivial enough: > > > > for s in ["asdfasdf.exe -u", "soemthing/else", > > "asdf.exe/qwerqwer/qwerqwer.exe"]: > > print `s`, > > i = s.find(".exe") > > print i, > > if i >= 0: > > s = s[:i+4] > > print `s` > > > > -- > > Grant Edwards grante Yow! Yow! It's some > > people > > at inside the wall! This is > >visi.combetter than mopping! -- http://mail.python.org/mailman/listinfo/python-list
Re: Adding a char inside path string
There was a typo. I corrected it. Hitesh wrote: > How about this: > > def TruncateString(s, Tindex): > return string.ljust(s,Tindex){:Tindex] > > > s = '\\serverName\\C:\\Folder Name1\\FolderName2\\example.exe -u ABC -g > > XYZ' > try: > Sindex = s.find(".exe") > if Sindex > 0: > Sindex = Sindex + 4 > s1 = TruncateString(s, Sindex) > except: > pass > > > hj > > > > Hitesh wrote: > > I post a crappy solution but I can add few more stuff to make it fail > > proof. > > i.e. I can search for ".exe -u" > > But if someone names folder like "folder.exe u". This script could > > fail. > > Or if in padded garbase I get ".exe u" > > > > These are two known issues I have to takcle. > > > > Thanks everyone for your help. > > > > > > > > Grant Edwards wrote: > > > On 2006-08-16, Hitesh <[EMAIL PROTECTED]> wrote: > > > > > > > anything after .exe should be truncated (or deleted). > > > > > > That will fail if any directory names contain the string > > > ".exe", but if that's what you want, it's trivial enough: > > > > > > for s in ["asdfasdf.exe -u", "soemthing/else", > > > "asdf.exe/qwerqwer/qwerqwer.exe"]: > > > print `s`, > > > i = s.find(".exe") > > > print i, > > > if i >= 0: > > > s = s[:i+4] > > > print `s` > > > > > > -- > > > Grant Edwards grante Yow! Yow! It's some > > > people > > > at inside the wall! This > > > is > > >visi.combetter than mopping! -- http://mail.python.org/mailman/listinfo/python-list
MS SQL Database connection
Hi currently I am using DNS and ODBC to connect to MS SQL database. Is there any other non-dns way to connect? If I want to run my script from different server I first have to create the DNS in win2k3. Thank you, hj -- http://mail.python.org/mailman/listinfo/python-list
Re: MS SQL Database connection
On Mar 5, 4:44 am, Tim Golden <[EMAIL PROTECTED]> wrote: > Hitesh wrote: > > Hi currently I am using DNS and ODBC to connect to MS SQL database. > > Is there any other non-dns way to connect? If I want to run my script > > from different server I first have to create the DNS in win2k3. > > Here are several ways to connect to an MSSQL database w/o > having to create "DNS" or anything else in win2k3 ;) > > There are other ways (the slightly stale MSSQL module > from Object Craft, for example, which still works fine > for Python <= 2.3). > > TJG > > > def adodbapi_connection (server, database, username, password): ># >#http://adodbapi.sf.net ># >import adodbapi >connectors = ["Provider=SQLOLEDB"] >connectors.append ("Data Source=%s" % server) >connectors.append ("Initial Catalog=%s" % database) >if username: >connectors.append ("User Id=%s" % username) >connectors.append ("Password=%s" % password) >else: >connectors.append("Integrated Security=SSPI") >return adodbapi.connect (";".join (connectors)) > > def pymssql_connection (server, database, username, password): ># >#http://pymssql.sf.net ># >import pymssql >if not username: > raise RuntimeError, "Unable to use NT authentication for pymssql" >return pymssql.connect (user=username, password=password, > host=server, database=database) > > def pyodbc_connection (server, database, username, password): ># >#http://pyodbc.sf.net ># >import pyodbc >connectors = ["Driver={SQL Server}"] >connectors.append ("Server=%s" % server) >connectors.append ("Database=%s" % database) >if username: >connectors.append ("UID=%s" % username) >connectors.append ("PWD=%s" % password) >else: >connectors.append ("TrustedConnection=Yes") >return pyodbc.connect (";".join (connectors)) > > Thank you. And I yes I meant DSN not DNS (my mistake, thank you for catching it ;) hj -- http://mail.python.org/mailman/listinfo/python-list
glob.glob output
import string import os f = open ("c:\\servername.txt", 'r') linelist = f.read() lineLog = string.split(linelist, '\n') lineLog = lineLog [:-1] #print lineLog for l in lineLog: path1 = "" + l + "\\server*\\*\\xtRec*" glob.glob(path1) When I run above from command line python, It prints the output of glob.glob but when I run it as a script, it does not print anything I know that there are files like xtRec* inside those folders.. and glob.glob does spits the path if run from python command line. I tried something like this but did not work: for l in lineLog: path1 = "" + l + "\\server*\\*\\xtRec*" xtRec = glob.glob(path1) print xtRec No results... xtRec = [] for l in lineLog: path1 = "" + l + "\\server*\\*\\xtRec*" xtrec = glob.glob(path1) print xtRec No luck here either. Seems like I am doing here something reallt silly mistake.. Thank you, hj -- http://mail.python.org/mailman/listinfo/python-list
Re: glob.glob output
On Mar 12, 1:58 pm, "Hitesh" <[EMAIL PROTECTED]> wrote: > import string > import os > > f = open ("c:\\servername.txt", 'r') > linelist = f.read() > > lineLog = string.split(linelist, '\n') > lineLog = lineLog [:-1] > #print lineLog > for l in lineLog: > path1 = "" + l + "\\server*\\*\\xtRec*" > glob.glob(path1) > > When I run above from command line python, It prints the output of > glob.glob but when I run it as a script, it does not print > anything I know that there are files like xtRec* inside those > folders.. and glob.glob does spits the path if run from python command > line. > > I tried something like this but did not work: > for l in lineLog: > path1 = "" + l + "\\server*\\*\\xtRec*" > xtRec = glob.glob(path1) > print xtRec > > No results... > > xtRec = [] > for l in lineLog: > path1 = "" + l + "\\server*\\*\\xtRec*" > xtrec = glob.glob(path1) > print xtRec > > No luck here either. > > Seems like I am doing here something reallt silly mistake.. > > Thank you, > hj I am using pythonWin and command line means Interactive Shell. -- http://mail.python.org/mailman/listinfo/python-list
Re: glob.glob output
On Mar 12, 2:12 pm, "Hitesh" <[EMAIL PROTECTED]> wrote: > On Mar 12, 1:58 pm, "Hitesh" <[EMAIL PROTECTED]> wrote: > > > > > > > import string > > import os > > > f = open ("c:\\servername.txt", 'r') > > linelist = f.read() > > > lineLog = string.split(linelist, '\n') > > lineLog = lineLog [:-1] > > #print lineLog > > for l in lineLog: > > path1 = "" + l + "\\server*\\*\\xtRec*" > > glob.glob(path1) > > > When I run above from command line python, It prints the output of > > glob.glob but when I run it as a script, it does not print > > anything I know that there are files like xtRec* inside those > > folders.. and glob.glob does spits the path if run from python command > > line. > > > I tried something like this but did not work: > > for l in lineLog: > > path1 = "" + l + "\\server*\\*\\xtRec*" > > xtRec = glob.glob(path1) > > print xtRec > > > No results... > > > xtRec = [] > > for l in lineLog: > > path1 = "" + l + "\\server*\\*\\xtRec*" > > xtrec = glob.glob(path1) > > print xtRec > > > No luck here either. > > > Seems like I am doing here something reallt silly mistake.. > > > Thank you, > > hj > > I am using pythonWin and command line means Interactive Shell.- Hide quoted > text - > > - Show quoted text - all right seems like I got it.. it's the looping part. I need to append the list. hj -- http://mail.python.org/mailman/listinfo/python-list
Re: glob.glob output
On Mar 12, 4:33 pm, Bruno Desthuilliers <[EMAIL PROTECTED]> wrote: > Hitesh a écrit : > > > import string > > import os > > > f = open ("c:\\servername.txt", 'r') > > linelist = f.read() > > > lineLog = string.split(linelist, '\n') > > lineLog = lineLog [:-1] > > #print lineLog > > for l in lineLog: > > path1 = "" + l + "\\server*\\*\\xtRec*" > > glob.glob(path1) > > And ? What are you doing then with return value of glob.glob ? > > BTW, seems like an arbitrarily overcomplicated way to do: > > from glob import glob > source = open(r"c:\\servername.txt", 'r') > try: >for line in source: > if line.strip(): >found = glob(r"\\%s\server*\*\xtRec*" % line) >print "\n".join(found) > finally: >source.close() > > > When I run above from command line python, It prints the output of > > glob.glob > > but when I run it as a script, it does not print > > anything > > Not without an explicit print statement. This behaviour is useful in the > interactive python shell, but would be really annoying in normal use. > > > I know that there are files like xtRec* inside those > > folders.. and glob.glob does spits the path if run from python command > > line. > > > I tried something like this but did not work: > > for l in lineLog: > > path1 = "" + l + "\\server*\\*\\xtRec*" > > xtRec = glob.glob(path1) > > print xtRec > > > No results... > > With the same source file, on the same filesystem ? Unless your xtRec* > files have been deleted between both tests you should have something here. > > > xtRec = [] > > for l in lineLog: > > path1 = "" + l + "\\server*\\*\\xtRec*" > > xtrec = glob.glob(path1) > > You're rebinding xtRec each turn. This is probably not what you want. Thank you for your reply. >From the return value I am trying to figure out whether the file xtRec* exist or not. I am newbie so exuse my ignorance... still learning. Somehow when I tried your solution, it takes 10mins to scan 200 plus servers but when I tried my code, it takes less then 2 mins. This is a puzzle for me, I gotta figure it out. hj -- http://mail.python.org/mailman/listinfo/python-list
Re: glob.glob output
On Mar 13, 11:00 am, Bruno Desthuilliers wrote: > Hitesh a écrit : > > > On Mar 12, 4:33 pm, Bruno Desthuilliers > > <[EMAIL PROTECTED]> wrote: > >> Hitesh a écrit : > > (snip) > > Thank you for your reply. > > From the return value I am trying to figure out whether the file > > xtRec* exist or not. > > Yes, I had understood this !-) > > What I wanted to point out was the fact you were discarding this value. > > > I am newbie so exuse my ignorance... still learning. > > Been here, done that... Don't worry. We're all still learning. > > > Somehow when I tried your solution, it takes 10mins to scan 200 plus > > servers but when I tried my code, > > Which one ? The original one, or the one where you first store glob > results in a list ? > > > it takes less then 2 mins. > > This is a puzzle for me, I gotta figure it out. > > i/o are usually expansive, so the difference may comes from the repeated > print. Also, I'm doing a bit of formatting, which is not your case. > > FWIW, my own snippet was just an example of the idiomatic way to read a > text file line by line. Doing TheRightThing(tm) for your actual problem > is your responsability !-) Thank you :-). I understand that now! hj -- http://mail.python.org/mailman/listinfo/python-list
List to string
Hi, I've a list like this.. str1 = ['this is a test string inside list'] I am doing it this way. for s in str1: temp_s = s print temp_s Any better suggestions? Thank you, hj -- http://mail.python.org/mailman/listinfo/python-list
Re: List to string
On Mar 18, 12:28 am, "Hitesh" <[EMAIL PROTECTED]> wrote: > Hi, > > I've a list like this.. > str1 = ['this is a test string inside list'] > > I am doing it this way. > > for s in str1: > temp_s = s > print temp_s > > Any better suggestions? > > Thank you, > hj I want to cast value of a list into string.. -- http://mail.python.org/mailman/listinfo/python-list
Re: List to string
On Mar 19, 8:11 am, Bruno Desthuilliers wrote: > Hitesh a écrit : > > > On Mar 18, 12:28 am, "Hitesh" <[EMAIL PROTECTED]> wrote: > >> Hi, > > >> I've a list like this.. > >> str1 = ['this is a test string inside list'] > > >> I am doing it this way. > > >> for s in str1: > >> temp_s = s > >> print temp_s > > Why this useless temp_s var ? > > > > >> Any better suggestions? > > >> Thank you, > >> hj > > > I want to cast value of a list into string.. > > There's no "cast" in Python. It would make no sens in a dynamically > typed language, where type informations belong to the LHS of a binding, > not the RHS. > > I guess that what you want is to build a string out of a list of > strings. If so, the answer is (assuming you want a newline between each > element of the list): > > print "\n".join(str1) Thank you guys. Yes that helped. :) hj -- http://mail.python.org/mailman/listinfo/python-list
Is there any better way to approach this problem?
Hi, I am newbie python dude. I need few hints. I created a DB table with following fields UserID(PK), TimeStamp, Version, IP, Port I need to insert these info from this .log file and the log file has lines like following with some junk garbase like "attempt using client software " I need to get rid of that. 10:03:54.527 (01) - User (xxx123) login attempt using client software version (1.0.1.1) from IP (xxx.xxx.xxx.xxx) port () Create a while 1 loop, read line from file.. disect line and insert 'stuff' into those variables and then run a query from inside the python to insert data... and do this untill EOF... Is there any better way to approach this problem? so if I have variable , let's say, TimeStamp, UserID, Version, IPAddress and PortNum.. how can I disect this line and insert into this variable? Thank you, hj -- http://mail.python.org/mailman/listinfo/python-list
What am I doing wrong here
Hi, I wanted to pass a popup mesage using windows messagin service to five PCs. If I just use following then PC1 gets the popup service message: import os os.system('net send PC1 "Message"') But if I try to create a for loop like this it doesn't work how can I pass computerName var as an argument? What am I doing wrong here? Thank you in advance import os Computerlist = ['PC1', 'PC2', 'PC3', 'PC4', 'PC5'] for ComputerName in Computerlist: print ComputerName os.system('net send ComputerName "Message"') -- http://mail.python.org/mailman/listinfo/python-list
Re: What am I doing wrong here
ok here is the deal... I figured out how to pass the variable but now messages are not popping up on the windows screen if I use this method: import os Computerlist = ['PC1', 'PC2', 'PC3', 'PC4', 'PC5'] for ComputerName in Computerlist: print ComputerName s = "net send %s" % ComputerName os.system('s "Message"') -- http://mail.python.org/mailman/listinfo/python-list
Re: What am I doing wrong here
Thank you Robert, It worked!!! Thank you so much -- http://mail.python.org/mailman/listinfo/python-list
Re: What am I doing wrong here
Thank you all for the quick replies. It worked! Truely appriciated. I am python novice and still learning I hope to contribute to this group someday :) Hitesh -- http://mail.python.org/mailman/listinfo/python-list
XML-SAX parser problem
Hello, Can any one help for error in following code. actually i want to map parent element with child element, but i am unable to do so. here is the code which i am trying for please do reply if iam not on right track. import xml.sax.handler class BookHandler(xml.sax.handler.ContentHandler): def __init__(self): self.inTitle1 = 0 self.inTitle2 = 0 self.mapping1 = {} self.mapping2 = {} def startElement(self, name, attributes="NULL"): #attributes="None" if name == "emph3": self.buffer1 = "" self.inTitle1 = 1 # self.id = attributes["None"] elif name == "year": self.buffer2 = "" self.inTitle2 = 1 def characters(self,data): if self.inTitle1 == 1: self.buffer1 += data elif self.inTitle2 == 1: self.buffer2 += data def endElement(self,name): if name == "year": self.inTitle2 = 0 self.mapping2[self.name] = self.buffer2 elif name =="emph3": self.inTitle1 =0 self.mapping1[self.name] = self.buffer1 # # #Jose Joaquin Avila #1929 # #Yiye Avila #1941 # # -- http://mail.python.org/mailman/listinfo/python-list