Re: Executing functions
Appreciate the responses, guys. I now see the difference between the ways I was trying to call function(s). R.D. -- http://mail.python.org/mailman/listinfo/python-list
Executing functions
Can someone help me understand why Example #1 & Example #2 will run the functions, while Example #3 DOES NOT? Thanks for your time! R.D. def One(): print "running fuction 1" def Two(): print "running fuction 2" def Three(): print "running fuction 3" # Example #1 fList = ["Two()","Three()"] for func in fList: exec func # Example #2 Two() Three() # Example #2 fList = ["Two()","Three()"] for func in fList: func -- http://mail.python.org/mailman/listinfo/python-list
Re: Too much code - slicing
On Sep 16, 2:47 pm, Benjamin Kaplan wrote: > On Thu, Sep 16, 2010 at 3:35 PM, DataSmash wrote: > > I need to create a simple utility to remove characters from either the > > right or left side of directories. > > This works, but there has to be a better way. I tried to use a > > variable inside the brackets but I can't get > > that to work. Can anyone think of a way to do this with less code? > > Thanks! > > > import os > > > dirs = filter(os.path.isdir, os.listdir('')) > > for dir in dirs: > > > # Left side > > > > The int() type will convert a string to an int for you. So all you > need to do is check the side and slice accordingly. > > if side=='l': > code = dir[int(num):] > else : > code = dir[:-1*int(num)] Much appreciated! I thought I tried every combination, guess I didn't try this as it works great. Thanks again. -- http://mail.python.org/mailman/listinfo/python-list
Too much code - slicing
I need to create a simple utility to remove characters from either the right or left side of directories. This works, but there has to be a better way. I tried to use a variable inside the brackets but I can't get that to work. Can anyone think of a way to do this with less code? Thanks! import os dirs = filter(os.path.isdir, os.listdir('')) for dir in dirs: # Left side if num == '1' and side == "l": code = dir[1:] if num == '2' and side == "l": code = dir[2:] if num == '3' and side == "l": code = dir[3:] if num == '4' and side == "l": code = dir[4:] if num == '5' and side == "l": code = dir[5:] if num == '6' and side == "l": code = dir[6:] if num == '7' and side == "l": code = dir[7:] if num == '8' and side == "l": code = dir[8:] if num == '9' and side == "l": code = dir[9:] if num == '10' and side == "l": code = dir[10:] # Right side if num == '1' and side == "r": code = dir[:-1] if num == '2' and side == "r": code = dir[:-2] if num == '3' and side == "r": code = dir[:-3] if num == '4' and side == "r": code = dir[:-4] if num == '5' and side == "r": code = dir[:-5] if num == '6' and side == "r": code = dir[:-6] if num == '7' and side == "r": code = dir[:-7] if num == '8' and side == "r": code = dir[:-8] if num == '9' and side == "r": code = dir[:-9] if num == '10' and side == "r": code = dir[:-10] print " Renaming "+dir+" to "+code os.rename(dir, code) -- http://mail.python.org/mailman/listinfo/python-list
Re: readline() & seek() ???
On Jun 5, 3:50 am, Carl Banks <[EMAIL PROTECTED]> wrote: > On Jun 4, 5:30 pm, DataSmash <[EMAIL PROTECTED]> wrote: > > > Hi group, > > I have a text file that contains thousands of lines and each line is > > 256 characters long. > > > This is my task: > > For each line in the file, move to the 25th character, if the > > character is a "T", > > move to the 35th character of the line and read 5 characters from > > there. > > Capture these 5 characters and write them to a new text file, each 5 > > characters separated by a comma. > > Your professor possibly reads comp.lang.python, and if so, is likely > to know how to track you down with your IP address. > > Carl Banks Marc, Thanks. Tim, Thanks for the code. It's a easy task IF you know what to look for. I didn't. Carl, I'm not a student. Was just looking for some ideas. -- http://mail.python.org/mailman/listinfo/python-list
readline() & seek() ???
Hi group, I have a text file that contains thousands of lines and each line is 256 characters long. This is my task: For each line in the file, move to the 25th character, if the character is a "T", move to the 35th character of the line and read 5 characters from there. Capture these 5 characters and write them to a new text file, each 5 characters separated by a comma. I appreciate your help! R.D. -- http://mail.python.org/mailman/listinfo/python-list
Re: list manipulation
Joe & Mike, Thanks for your input! R.D. -- http://mail.python.org/mailman/listinfo/python-list
list manipulation
Hello, I have a list that looks like this: roadList = ["Motorways","Local","Arterial"] I want to apply some code so that the output looks like this: "Motorways;Local;Arterial" ...in other words, I want each item in the list separated by a ';' and then the whole thing surrounded by quotes. How can this be done with the LEAST amount of code? I appreciate your help! R.D. -- http://mail.python.org/mailman/listinfo/python-list
Re: linux disc space
On Feb 15, 1:32 pm, Jeff Schwab <[EMAIL PROTECTED]> wrote: > Chris wrote: > > On Feb 15, 7:10 pm, DataSmash <[EMAIL PROTECTED]> wrote: > >> I simply want to capture the free disc space in a variable so that I > >> can compare changes. I'm aware of a few commands like "df -h" or "du - > >> k", but I can't figure out how to capture those values as a variable. > >> I also looked at os.statvfs(), but that output doesn't seem to make > >> any sense at all to me, knowing the size of the disc. > >> Thanks for your help! > >> R.D. > > > import os, statvfs > > s = os.statvfs(".") > > freebytes = s[statvfs.F_BSIZE] * s[statvfs.F_BAVAIL] > > Is it worth distinguishing free bytes from available bytes? I've never > seen them differ, and I'm not sure how they ever would... > > import os > import statvfs > > def free_bytes(path): > stats = os.statvfs(path) > return stats[statvfs.F_BSIZE] * stats[statvfs.F_BFREE] > > def avail_bytes(path): > stats = os.statvfs(path) > return stats[statvfs.F_BSIZE] * stats[statvfs.F_BAVAIL] > > if __name__ == '__main__': > import sys > for path in sys.argv[1:]: > print "%s:" % path, > print "%dK free," % (free_bytes(path) / 1024), > print "%dK available" % (avail_bytes(path) / 1024) Chris, Much thanks. That's just what I need. Jeff, Not sure what's taking up the "available" space, but it's about 12GB on my system. Interesting. Thanks. -- http://mail.python.org/mailman/listinfo/python-list
linux disc space
I simply want to capture the free disc space in a variable so that I can compare changes. I'm aware of a few commands like "df -h" or "du - k", but I can't figure out how to capture those values as a variable. I also looked at os.statvfs(), but that output doesn't seem to make any sense at all to me, knowing the size of the disc. Thanks for your help! R.D. -- http://mail.python.org/mailman/listinfo/python-list
Re: moving multiple directories
Thanks Larry, I'll give that a try... R.D. > > Use win32.moveFile method instead. This links directly to the Windows > MoveFile method that just moves the directory entries around. > > From Win32 Documentation: > > win32api.MoveFile > MoveFile(srcName, destName) > > Renames a file, or a directory (including its children). > > Parameters > > srcName : string > > The name of the source file. > > destName : string > > The name of the destination file. > > Comments > This method can not move files across volumes. > > -Larry -- http://mail.python.org/mailman/listinfo/python-list
moving multiple directories
Hi, I need to organize thousands of directories full of files. I want to move these directories into other subdirectories. For example, all the directories that start with 01, move to a directory named "one", all directories that start with 02, move to a directory name "two", and so on I can't seem to find any easy way to do this. Looks like shutil.move only lets you move if the subdirectory DOES NOT exist, so after the first directory moves, the script blows up on the second move. I guess you could use shutil.copy or shutil.copytree but then you have to delete as well. Much longer process when you have hundreds of gigabytes of data. Thanks for your help! R.D. -- http://mail.python.org/mailman/listinfo/python-list
Re: Search & Replace
Really appreciate all the all the different answers and learning tips! -- http://mail.python.org/mailman/listinfo/python-list
Search & Replace
Hello, I need to search and replace 4 words in a text file. Below is my attempt at it, but this code appends a copy of the text file within itself 4 times. Can someone help me out. Thanks! # Search & Replace file = open("text.txt", "r") text = file.read() file.close() file = open("text.txt", "w") file.write(text.replace("Left_RefAddr", "FromLeft")) file.write(text.replace("Left_NonRefAddr", "ToLeft")) file.write(text.replace("Right_RefAddr", "FromRight")) file.write(text.replace("Right_NonRefAddr", "ToRight")) file.close() -- http://mail.python.org/mailman/listinfo/python-list
Re: os.chdir doesn't accept variables sometimes
A simple way to get all the files throughout the directory sturcture... You may have to rewrite the "CONVERTING" part. import os, glob for root, dirs, files in os.walk(os.getcwd()): for file in files: if file.endswith(".mp3"): print "File: " + os.path.abspath(os.path.join(root, file)) print "CONVERTING "+file+" to "+file[:file.index(".")]+".mp3" file = file[:file.index(".")]+".mp3" Donn Cave wrote: > In article <[EMAIL PROTECTED]>, > "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > > > #!/usr/bin/python > > > > from os import * > > > > chdir("/home/chainlynx/Desktop/Music") > > for artist in listdir(getcwd()): > > print "===ARTIST: "+artist > > chdir(artist) > > for album in listdir(getcwd()): > > print "---ALBUM: "+album > > print "CWD: " + getcwd() > > chdir(album) ##ERROR ON THIS > > LINE > > for string in listdir(album): > ... > > > Traceback (most recent call last): > > File "/home/chainlynx/workspace/PyTest/src/pypack/__init__.py", line > > 12, in ? > > for string in listdir(album): > > OSError: [Errno 2] No such file or directory: 'Album1' > > To start with, note that your traceback implicates the listdir() > on line 12, not the chdir() before it. This listdir() uses the > same parameter as that preceding chdir(), that appears to be your > problem. > > One of your problems, anyway. You're doing a lot of downwards > chdirs, but no upwards, which is going to limit the extent of > your directory traversal. > > The "from os import *" is a terrible idea, where did you get that? > "os" has a lot of identifiers in it that tend to collide with other > namespaces. "open" is a classic example. Don't do that, with "os" > or generally any module. > > As a more general direction, it would be a good idea to look > into standard library functions, e.g., os.path.walk > > > P.S. Bonus points: is there any way to bash shell script this on the > > command line instead (recursively)? > > Depends on what you want it to do, but maybe something like > > find . -name \*.mp3 -exec $HOME/bin/cvt .mp4 {} \; > > where cvt would be something like >#!/bin/sh >case $1:$2 in >.mp4:*.mp3) mp3_to_mp4 $2 ${2%.mp3}.mp4 ;; >... > > You'd have to think about it. > >Donn Cave, [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: Linking onClick event to other controls on the Frame
Hi Miki, I appreciate you taking the time to figure out what I was trying to do. This is exactly what I was looking for. I plan on studing this code and looking at the wxpython demo as you mentioned. Thanks again! R.D. Harles -- http://mail.python.org/mailman/listinfo/python-list
Linking onClick event to other controls on the Frame
Hello, I've created a simple form with 2 radio boxes, 2 text boxes and a button. When I click the button, I'd like to write each "choice" to a text file. I can't figure out how to "link" the onClick event to the other 4 controls. Any help would be much appreciated! R.D. Harles import wx, sys class Form1(wx.Panel): def __init__(self, parent, id): wx.Panel.__init__(self, parent, id) # Create button self.button =wx.Button(self, 50, "GO!", wx.Point(20, 200)) wx.EVT_BUTTON(self, 50, self.OnClick) ## First name # Edit control self.lblname = wx.StaticText(self, -1, "First Name :",wx.Point(20,50)) self.editname = wx.TextCtrl(self, 10, "", wx.Point(90, 50), wx.Size(100,-1)) wx.EVT_TEXT(self, 10, self.EvtText) ## Last Name # Edit control self.lblname = wx.StaticText(self, -1, "Last Name :",wx.Point(20,75)) self.editname = wx.TextCtrl(self, 20, "", wx.Point(90, 75), wx.Size(100,-1)) wx.EVT_TEXT(self, 20, self.EvtText) # Radio Boxes self.radioList = ['Employed', 'Unemployed'] rb = wx.RadioBox(self, 30, "Status:", wx.Point(20, 100), wx.DefaultSize, self.radioList, 2, wx.RA_SPECIFY_COLS) wx.EVT_RADIOBOX(self, 30, self.EvtRadioBox) # Radio Boxes self.radioList = ['Married', 'Single'] rb = wx.RadioBox(self, 40, "Status:", wx.Point(20, 150), wx.DefaultSize, self.radioList, 2, wx.RA_SPECIFY_COLS) wx.EVT_RADIOBOX(self, 40, self.EvtRadioBox) # Text event def EvtText(self, event): print event.GetString() # RadioBox event def EvtRadioBox(self, event): print event.GetId() # Button event def OnClick(self,event): print "Writing job.cfg..." file = open("job.cfg", "w") file.write("") app = wx.PySimpleApp() frame = wx.Frame(None, -1, " Questions") Form1(frame,-1) frame.Show(1) app.MainLoop() -- http://mail.python.org/mailman/listinfo/python-list
Re: unzip zip files
Thanks! I ended up using the "-d" parameter. I did try the zipfile module but I couldn't figure it out, nor could I find any examples using it. I also didn't have any luck changing the working dircectory and making it work. import subprocess, os # Get all the zip files in the current directory. for zip in os.listdir(''): if zip.endswith(".zip"): # Remove the first 3 and the last 4 characters # e.g. usa12345.zip becomes 12345 zipBase = zip[3:-4] # Make directory for unzipping os.mkdir(zipBase) # Make system call "unzip" print "\n unzip -d", zipBase, zip subprocess.Popen(["unzip", "-d", zipBase, zip]).wait() -- http://mail.python.org/mailman/listinfo/python-list
unzip zip files
I need to unzip all zip file(s) in the current directory into their own subdirectories. The zip file name(s) always start with the string "usa" and end with ".zip". The code below will make the subdirectory, move the zip file into the subdirectory, but unzips the contents into the root (current) directory. I want the contents of the zip file unloaded into the newly created subdirectory where the zip file is. Any ideas? Thanks. R.D. import subprocess # Get all the zip files in the current directory. for zip in os.listdir(''): if zip.endswith(".zip"): # Remove the first 3 and the last 4 characters # e.g. usa12345.zip becomes 12345 zipBase = zip[3:-4] # Make directory for unzipping os.mkdir(zipBase) # Move the zip file to the subdirectory shutil.move(zip, zipBase) # Make system call "unzip" subprocess.Popen(["unzip", zipBase + "\\" + zip]).wait() -- http://mail.python.org/mailman/listinfo/python-list
front end GUI
I'm looking for software to create a simple GUI to execute python code for users in the office. The GUI would need a couple text boxes for user input and some option buttons and check boxes. Can anyone recommend free software that doesn't require a huge learning curve? My python skills are probably a 3 or 4 on a scale from 1 to 10. Thanks for your help! R.D. -- http://mail.python.org/mailman/listinfo/python-list
Re: Confused: appending to a list
Thanks for explaining and all the additional ideas! R.D. -- http://mail.python.org/mailman/listinfo/python-list
Confused: appending to a list
I'm confused. Why is it that when I say "while len(list) < 5:", I get 5 items in my list. If I say "while len(list) < 6:", I get 6 items in the list and so on. I would think if I said "less than 5", I would get 4 items. Can anyone explain this? Thanks. R.D. # Start an empty list list = [] while len(list) < 5: # Get a random number between 1 & 100 num = random.randint(1,100) # Make sure there are no duplicates if num not in list: # Append each number to the list list.append(num) print list -- http://mail.python.org/mailman/listinfo/python-list
Re: import random module
Much Thanks! I deleted the random.pyc and renamed the script and everything is good! R.D. -- http://mail.python.org/mailman/listinfo/python-list
Re: import random module
Much Thanks! I deleted the random.pyc and renamed the script and everything is good! R.D. -- http://mail.python.org/mailman/listinfo/python-list
import random module
Hi, When I import the random module at the python interpreter, it works fine: >>> import random >>> x = random.randint(1,55) >>> print x 14 >>> BUT, when I put the same code in a python script: * random.py: import random x = random.randint(1,55) print x and run it at the command line, I get: Traceback (most recent call last): File p:\temp\random.py, line 7, in ? import random File p:\temp\random.py, line 8, in ? x = random.randint(1,55) AttributeError: 'module" object has no attribut 'randint' I run scripts at the command line everyday so there must be something specifically wrong with the "random" module, unless I'm totally missing something here. Any ideas? Thanks, R.D. -- http://mail.python.org/mailman/listinfo/python-list
Re: batch mkdir using a file list
Awesome! That worked! Much thanks to Peter and all of you who took the time to answer my question. R.D. -- http://mail.python.org/mailman/listinfo/python-list
Re: batch mkdir using a file list
OR...if python can't handle this type of text file, what do I need to do to remove the "\n" from the file? -- http://mail.python.org/mailman/listinfo/python-list
Re: batch mkdir using a file list
I am using bash shell on windows and I'm getting the error: TypeError: loop over non-sequence Is there any way around not messing with the text file. I want to batch generate the text lists as well. Thanks! -- http://mail.python.org/mailman/listinfo/python-list
batch mkdir using a file list
Hello, I think I've tried everything now and can't figure out how to do it. I want to read in a text list from the current directory, and for each line in the list, make a system directory for that name. My text file would look something like this: 1144 1145 1146 1147 I simply want to create these 4 directories. It seems like something like the following code should work, but it doesn't. import os file = open("list.txt", "r") read = file.read() print "Creating directory " + str(read) os.mkdir(str(read)) Appreciate any help you can give! R.D. Harles -- http://mail.python.org/mailman/listinfo/python-list