Newbie question regarding string.split()
Hey guys, So I have a question regarding the split() function in the string module. Let's say I have an string... input = "2b 3 4bx 5b 2c 4a 5a 6" projectOptions = (input.replace(" ", "")).split('2') print projectOptions ['', 'b34bx5b', 'c4a5a6'] My question is, why is the first element of projectOptions an empty string? What can I do so that the first element is not an empty string? but the 'b34bx5b' string as I expected? Thanks so much guys. :) -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbie question regarding string.split()
On Apr 21, 3:30 pm, Bruno Desthuilliers <[EMAIL PROTECTED]> wrote: > kevinliu23 a écrit :> Hey guys, > > > So I have a question regarding the split() function in the string > > module. Let's say I have an string... > > > input = "2b 3 4bx 5b 2c 4a 5a 6" > > projectOptions = (input.replace(" ", "")).split('2') Thanks for all your help everyone. :) > The parens around the call to input.replace are useless: >projectOptions = input.replace(" ", "").split('2') > > > print projectOptions > > > ['', 'b34bx5b', 'c4a5a6'] > > (snip) > > > What can I do so that the first element is not an empty > > string? but the 'b34bx5b' string as I expected? > > projectOptions = filter(None, input.replace(" ", "").split('2')) -- http://mail.python.org/mailman/listinfo/python-list
Best way to wait for string input
Hi guys, Python newbie here for some expert help. So basically I want to design a menu system that waits for a string input. I'm not sure what the best way of going about this is. The current system waits for a single character input using msvcrt.kbhit( ) and msvcrt.getch( ). Is there something equivalent to wait for string inputs? -- http://mail.python.org/mailman/listinfo/python-list
Inserting '-' character in front of all numbers in a string
Hey guys, I want to be able to insert a '-' character in front of all numeric values in a string. I want to insert the '-' character to use in conjunction with the getopt.getopt() function. Rigt now, I'm implementing a menu system where users will be able to select a set of options like "2a 3ab" which corresponds to menu choices. However, with getopt.getopt(), it'll only return what I want if I input -2a -3ab as my string. I don't want the user have to insert a '-' character in front of all their choices, so I was thinking of accepting the string input first, then adding in the '-' character myself. So my qusetion is, how do I change: "2a 3ab" into "-2a -3ab". Regular expressions? :/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Inserting '-' character in front of all numbers in a string
Hey guys, thanks for the quick replies. I'm looking for something more generic than adding it to "2a 3ab". For example, under the menu option 2, there can be upwards of 8 other suboptions. I'll see what's suggested here and post back if I run into more problems. Thanks guys! -- http://mail.python.org/mailman/listinfo/python-list
How to check for remaining hard drive space in Windows?
HI, I am new to Python and wanted to know how to check for the remaining disk space on my Windows machine using Python? I was thinking of using the command line "dir" and trying to extract the output from there. But I'm not sure how to extract command line strings using Python either. Anyway help would be appreciated. :) -- http://mail.python.org/mailman/listinfo/python-list
Re: How to check for remaining hard drive space in Windows?
Thanks so much for the help guys. I got the code Sick Monkey provided to work on my computer. Now I"m more confused than ever though. :) I thought the only standard modules provided by Python are listed here: http://docs.python.org/modindex.html But it appears that there are other modules available to me without having to download third party code. Could someone point me to the documentation of these other modules such as win32com.client? Thanks everyone for your help. :) Also, how could I get the computer name? I would be running this code on another machine and don't want to change the computer name in the code every time I port it to another computer. Thanks! Kevin On Feb 28, 4:16 pm, Tim Golden <[EMAIL PROTECTED]> wrote: > [... re getting free disk space ...] > > Sick Monkey wrote: > > Here you are: > > > >>> from win32com.client import GetObject > wmiObj = GetObject("winmgmts:MGW01641\\root\\cimv2") > diskinfo = wmiObj.ExecQuery("Select * from Win32_LogicalDisk") > for disk in diskinfo: > > ...print disk.Name, disk.FreeSpace > > ... > > A: None > > C: 16978259968 > > D: None > > Well it's not often someone beats me to a WMI > solution :) Just to be different, you can also > look at the GetDiskFreeSpace function in the > win32api module of the pywin32 extensions. > > The doc says: """ > tuple = GetDiskFreeSpace(rootPath) > > Retrieves information about the specified disk, including the amount of > free space available. > > Parameters > > rootPath : string > > Specifies the root directory of the disk to return information about. If > rootPath is None, the method uses the root of the current directory. > > Win32 API References > > Search for GetDiskFreeSpace at msdn, google or google groups. > > Return Value > The return value is a tuple of 4 integers, containing the number of > sectors per cluster, the number of bytes per sector, the total number of > free clusters on the disk and the total number of clusters on the disk. > If the function fails, an error is returned. > """ > > TJG -- http://mail.python.org/mailman/listinfo/python-list
Re: How to check for remaining hard drive space in Windows?
Just tried your solution Tim, worked like a charm. :) It's great because I don't even have to worry about the computer name. A question regarding the rootPath parameter...how would I be passing it? Would I be passing it as... tuple = win32api.GetDiskFreeSpace(r'C:') or just leave it blank and the function will automatically use the rootPath of where the .py file resides? Both have returned the correct result. Kevin On Feb 28, 4:24 pm, "kevinliu23" <[EMAIL PROTECTED]> wrote: > Thanks so much for the help guys. I got the code Sick Monkey provided > to work on my computer. Now I"m more confused than ever though. :) I > thought the only standard modules provided by Python are listed here: > > http://docs.python.org/modindex.html > > But it appears that there are other modules available to me without > having to download third party code. Could someone point me to the > documentation of these other modules such as win32com.client? Thanks > everyone for your help. :) > > Also, how could I get the computer name? I would be running this code > on another machine and don't want to change the computer name in the > code every time I port it to another computer. Thanks! > > Kevin > > On Feb 28, 4:16 pm, Tim Golden <[EMAIL PROTECTED]> wrote: > > > [... re getting free disk space ...] > > > Sick Monkey wrote: > > > Here you are: > > > > >>> from win32com.client import GetObject > > >>>> wmiObj = GetObject("winmgmts:MGW01641\\root\\cimv2") > > >>>> diskinfo = wmiObj.ExecQuery("Select * from Win32_LogicalDisk") > > >>>> for disk in diskinfo: > > > ...print disk.Name, disk.FreeSpace > > > ... > > > A: None > > > C: 16978259968 > > > D: None > > > Well it's not often someone beats me to a WMI > > solution :) Just to be different, you can also > > look at the GetDiskFreeSpace function in the > > win32api module of the pywin32 extensions. > > > The doc says: """ > > tuple = GetDiskFreeSpace(rootPath) > > > Retrieves information about the specified disk, including the amount of > > free space available. > > > Parameters > > > rootPath : string > > > Specifies the root directory of the disk to return information about. If > > rootPath is None, the method uses the root of the current directory. > > > Win32 API References > > > Search for GetDiskFreeSpace at msdn, google or google groups. > > > Return Value > > The return value is a tuple of 4 integers, containing the number of > > sectors per cluster, the number of bytes per sector, the total number of > > free clusters on the disk and the total number of clusters on the disk. > > If the function fails, an error is returned. > > """ > > > TJG -- http://mail.python.org/mailman/listinfo/python-list
Re: How to check for remaining hard drive space in Windows?
H, right now...I'm doing multiplication on the index values returned by GetDiskFreeSpace. According to the documentation... tuple[0]: sectors per cluster tuple[1]: number of bytes per sector tuple[2]: total number of free clusters tuple[3]: total number of clusters on the disk So I'm multiplying together indices 0, 1 and 2 together to get the amount of free space left on my hard drive in bytes. The product of the first three indices is over 10 gigabytes and is correct according to my calculations. Why would the documentation say it does not return data over 2 gigabytes then? Or do you mean not over 2 gigabytes returned for tuple[2]? I believe my tuple[2] returned a value of 2778727 bytes, which is well below the 2 gigabyte capacity. Anyway, thanks for letting me know about GetDiskFreeSpaceEx. I will look into this function as well. Thanks guys! On Feb 28, 5:08 pm, "Jerry Hill" <[EMAIL PROTECTED]> wrote: > On 2/28/07, Tim Golden <[EMAIL PROTECTED]> wrote: > > > Well it's not often someone beats me to a WMI > > solution :) Just to be different, you can also > > look at the GetDiskFreeSpace function in the > > win32api module of the pywin32 extensions. > > The MSDN page for that function warns: > "The GetDiskFreeSpace function cannot report volume sizes that are > greater than 2 gigabytes (GB). To ensure that your application works > with large capacity hard drives, use the GetDiskFreeSpaceEx function." > > Make sure to keep that in mind if you're recording free disk space > someplace, rather than just checking for enough space to do an install > or something. > > -- > Jerry -- http://mail.python.org/mailman/listinfo/python-list
Re: How to check for remaining hard drive space in Windows?
Thanks Tim, I only need to run the get hard drive space function on one drive for one machine so I'll stick to GetDiskFreeSpace. If I need to expand this feature to multiple harddrives/machines, I'll be sure to come back to this thread. :) Kevin On Mar 1, 4:17 am, Tim Golden <[EMAIL PROTECTED]> wrote: > kevinliu23 wrote: > > Just tried your solution Tim, worked like a charm. :) > > > It's great because I don't even have to worry about the computer name. > > A question regarding the rootPath parameter...how would I be passing > > it? Would I be passing it as... > > >tuple = win32api.GetDiskFreeSpace(r'C:') > > or just leave it blank and the function will automatically use the > > rootPath of where the .py file resides? > > > Both have returned the correct result. > > The simple answer is: I'm not sure. If you experiment and find > something which works, just use it! > > Something which SickMonkey made me return to your question. > Are you trying to find the disk space available on a > different machine (possibly on several different machines)? > If so, then WMI is definitely your answer. You can > run -- from your machine -- one piece of code which > will attach to several different machines to give > you the answer. (as per Sick Monkey's later post). > > If I've read too much into your question, well nothing's > ever wasted on the internet. Here's some sample code > which uses the wmi module from: > >http://timgolden.me.uk/python/wmi.html > > > machines = ['mycomp', 'othercomp'] > > for machine in machines: >print "Machine:", machine >c = wmi.WMI (machine) ># only consider local fixed disks >for disk in c.Win32_LogicalDisk (DriveType=3): > print disk.Name, disk.FreeSpace >print > > > > Yes, you could even write: > >for disk in wmi.WMI (machine).Win32_LogicaDisk (DriveType=3): > > but I'd find it a touch unwieldy. YMMV. > > HTH > TJG -- http://mail.python.org/mailman/listinfo/python-list