Re: changing a file's permissions

2006-10-02 Thread oscartheduck
> This is an attribute of the file (an object in the filesystem) which
> is checked by the kernel before allowing the file to be
> executed. Python has nothing to do with this; if the attributes allow
> execution, you can execute it as a program; if not, you can't.
>

I took this to heart and changed my question to "How do I run shell
commands within a python script?" I was taking my lead from Perl, which
has the infamous backquotes, and though the two languages differ in
conceptive aim, I discovered that shell commands are accessible in
python:

http://pyre.third-bit.com/pipermail/2005fall/2005-October/000228.html




> Incidentally, if the program is intended to be run from a command
> line, it's best to name the file with no '.py' extension. The fact
> that a command is implemented in Python is irrelevant to the person
> running that command; it also means you can implement it in some other
> language later on without changing everything that uses that command.

How I've been doing it inside my script (which is a script generation
tool, nicely enough) is creating symlinks with no extension on them in
a directory in $PATH that the user has read/write/execute access to.
It's a hidden directory inside the user's home folder that I added to
the end of the path.

-- 
http://mail.python.org/mailman/listinfo/python-list


regular expression concatenation with strings

2007-06-22 Thread oscartheduck
Hi folks,

I have a little script that sits in a directory of images and, when
ran, creates thumbnails of the images. It works fine if I call the
function inside the program with something like "thumbnailer("jpg),
but I want to use a regular expression instead of a plain string so
that I can match jpeg, jpg, JPEG etc.



Here's the script:

---

 #!/usr/bin/env python
from PIL import Image
import glob, os, re

size = 128, 128

# takes an extension (e.g. jpg, png, gif, tiff) as argument
def thumbnailer(extension):
#glob the directory the script is in for files of the type
foo.extension
  for picture in glob.glob("*." + extension):
file, ext = os.path.splitext(picture)
im = Image.open (picture)
im.thumbnail(size, Image.ANTIALIAS)
im.save(file + ".thumbnail." + extension)


jpg = re.compile("jpg|jpeg", re.IGNORECASE)
thumbnailer(jpg)



---

And here's the error:

---

Traceback (most recent call last):
  File "./thumbnail.py", line 19, in ?
thumbnailer(jpg)
  File "./thumbnail.py", line 11, in thumbnailer
for picture in glob.glob("*." + extension):
TypeError: cannot concatenate 'str' and '_sre.SRE_Pattern' objects

---


It looks to me like the conversion to a regex object instead of a
plain string is screwing up the file glob + extension concatenation.
Is there a simple way to accomplish what I'm trying to do here and get
rid of that error?

Thanks!

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: regular expression concatenation with strings

2007-06-22 Thread oscartheduck
Hi,

I noticed a small error in the code (you referenced extension, which
you had renamed to filenameRx), and when I corrected it I received the
original error again. What was it you were trying to do to solve the
problem, though?

Thanks!



On Jun 22, 2:41 pm, Jacek Trzmiel <[EMAIL PROTECTED]> wrote:
> Hi,
>
> oscartheduck wrote:
> > I have a little script that sits in a directory of images and, when
> > ran, creates thumbnails of the images. It works fine if I call the
> > function inside the program with something like "thumbnailer("jpg),
> > but I want to use a regular expression instead of a plain string so
> > that I can match jpeg, jpg, JPEG etc.
>
> Something like this will work:
>
> #!/usr/bin/env python
> #from PIL import Image
> import glob, os, re
>
> size = 128, 128
>
> def thumbnailer(dir, filenameRx):
>  for picture in [ p for p in os.listdir(dir) if
> os.path.isfile(os.path.join(dir,p)) and filenameRx.match(p) ]:
>  file, ext = os.path.splitext(picture)
>  im = Image.open (picture)
>  im.thumbnail(size, Image.ANTIALIAS)
>  im.save(file + ".thumbnail." + extension)
>
> jpg = re.compile(".*\.(jpg|jpeg)", re.IGNORECASE)
> thumbnailer(".", jpg)
>
> Best regards,
> Jacek.




-- 
http://mail.python.org/mailman/listinfo/python-list


Re: regular expression concatenation with strings

2007-06-22 Thread oscartheduck


Shoot, I think I realised what I'm doing wrong.

Let me write some code to address this, but I'm almost certain that
the error is that I'm attempting to save an image with a regular
expression, which is by nature fluid, tacked on to its ass after
the .thumbnail. Which, now I look at your code, you address implicitly
by suggesting that the extension be a different variable.


Thanks!





On Jun 22, 3:10 pm, oscartheduck <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I noticed a small error in the code (you referenced extension, which
> you had renamed to filenameRx), and when I corrected it I received the
> original error again. What was it you were trying to do to solve the
> problem, though?
>
> Thanks!
>
> On Jun 22, 2:41 pm, Jacek Trzmiel <[EMAIL PROTECTED]> wrote:
>
> > Hi,
>
> > oscartheduck wrote:
> > > I have a little script that sits in a directory of images and, when
> > > ran, creates thumbnails of the images. It works fine if I call the
> > > function inside the program with something like "thumbnailer("jpg),
> > > but I want to use a regular expression instead of a plain string so
> > > that I can match jpeg, jpg, JPEG etc.
>
> > Something like this will work:
>
> > #!/usr/bin/env python
> > #from PIL import Image
> > import glob, os, re
>
> > size = 128, 128
>
> > def thumbnailer(dir, filenameRx):
> >  for picture in [ p for p in os.listdir(dir) if
> > os.path.isfile(os.path.join(dir,p)) and filenameRx.match(p) ]:
> >  file, ext = os.path.splitext(picture)
> >  im = Image.open (picture)
> >  im.thumbnail(size, Image.ANTIALIAS)
> >  im.save(file + ".thumbnail." + extension)
>
> > jpg = re.compile(".*\.(jpg|jpeg)", re.IGNORECASE)
> > thumbnailer(".", jpg)
>
> > Best regards,
> > Jacek.


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: regular expression concatenation with strings

2007-06-22 Thread oscartheduck
Got it:

#!/usr/bin/env python
from PIL import Image
import glob, os, re

size = 128, 128

def thumbnailer(dir, filenameRx):
 for picture in [ p for p in os.listdir(dir) if
os.path.isfile(os.path.join(
dir,p)) and filenameRx.match(p) ]:
 file, ext = os.path.splitext(picture)
 im = Image.open (picture)
 im.thumbnail(size, Image.ANTIALIAS)
 im.save(file + ".thumbnail" + ext)

jpg = re.compile(".*\.(jpg|jpeg)", re.IGNORECASE)
thumbnailer(".", jpg)



The answer was sitting in front of my eyes. What is your code doing?
It looks like:

for $foo in [current working directory]
if $foo is a file
and foo's name matches the regex



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: regular expression concatenation with strings

2007-06-22 Thread oscartheduck
Oh dear. I just want to make it clear I wasn't trying to take credit
for the change of extension to ext. I genuinely worked it out
independently and then rushed back and posted about it, but you
definitely worked it out and wrote it up first!

Sorry! And thanks for the help!!



James

On Jun 22, 4:07 pm, oscartheduck <[EMAIL PROTECTED]> wrote:
> Got it:
>
> #!/usr/bin/env python
> from PIL import Image
> import glob, os, re
>
> size = 128, 128
>
> def thumbnailer(dir, filenameRx):
>  for picture in [ p for p in os.listdir(dir) if
> os.path.isfile(os.path.join(
> dir,p)) and filenameRx.match(p) ]:
>  file, ext = os.path.splitext(picture)
>  im = Image.open (picture)
>  im.thumbnail(size, Image.ANTIALIAS)
>  im.save(file + ".thumbnail" + ext)
>
> jpg = re.compile(".*\.(jpg|jpeg)", re.IGNORECASE)
> thumbnailer(".", jpg)
>
> The answer was sitting in front of my eyes. What is your code doing?
> It looks like:
>
> for $foo in [current working directory]
> if $foo is a file
> and foo's name matches the regex


-- 
http://mail.python.org/mailman/listinfo/python-list


regular expressions eliminating filenames of type foo.thumbnail.jpg

2007-06-24 Thread oscartheduck
Hi folks,

I'm trying to alter a program I posted about a few days ago. It
creates thumbnail images from master images. Nice and simple.

To make sure I can match all variations in spelling of jpeg, and
different cases, I'm using regular expressions.


The code is currently:

-

#!/usr/bin/env python
from PIL import Image
import glob, os, re

size = 128, 128

def thumbnailer(dir, filenameRx):
 for picture in [ p for p in os.listdir(dir) if
os.path.isfile(os.path.join(
dir,p)) and filenameRx.match(p) ]:
 file, ext = os.path.splitext(picture)
 im = Image.open (picture)
 im.thumbnail(size, Image.ANTIALIAS)
 im.save(file + ".thumbnail" + ext)

jpg = re.compile(".*\.(jpg|jpeg)", re.IGNORECASE)
thumbnailer(".", jpg)

-

The problem is this. This code outputs foo.thumbnail.jpg when ran, and
when ran again it creates foo.thumbnail.thumbnail.jpg and so on,
filling a directory.

The obvious solution is to filter out any name that contains the term
"thumbnail", which I can once again do with a regular expression. My
problem is the construction of this expression.

The relevant page in the tutorial docs is: 
http://docs.python.org/lib/re-syntax.html

It lists (?>> m = re.search('(?http://mail.python.org/mailman/listinfo/python-list


Re: regular expressions eliminating filenames of type foo.thumbnail.jpg

2007-06-25 Thread oscartheduck
I eventually went with:

#!/usr/bin/env python
from PIL import Image
import glob, os, re

size = 128, 128

def thumbnailer(dir, filenameRx):
 for picture in [ p for p in os.listdir(dir) if
os.path.isfile(os.path.join(
 dir,p)) and filenameRx.match(p) if 'thumbnail' not in p]:
 file, ext = os.path.splitext(picture)
 im = Image.open (picture)
 im.thumbnail(size, Image.ANTIALIAS)
 im.save(file + ".thumbnail" + ext)

jpg = re.compile(".*\.(jpg|jpeg)", re.IGNORECASE)
thumbnailer(".", jpg)


Thanks for the help!



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: regular expressions eliminating filenames of type foo.thumbnail.jpg

2007-06-25 Thread oscartheduck
Well, darn.

I just discovered that the computer this is going to run on only has
python version 2.2 installed on it, which apparently doesn't support
checking a whole word, but instead checks a single letter against
other single letters. 2.4 and, presumably 2.5 though I've actually not
used it much, has such huge leaps forwards relative to 2.2 that it's
frightening thinking about where the language is going.

But for now, I'm going to have to work on this problem again from
scratch, it seems.



On Jun 25, 3:41 pm, oscartheduck <[EMAIL PROTECTED]> wrote:
> I eventually went with:
>
> #!/usr/bin/env python
> from PIL import Image
> import glob, os, re
>
> size = 128, 128
>
> def thumbnailer(dir, filenameRx):
>  for picture in [ p for p in os.listdir(dir) if
> os.path.isfile(os.path.join(
>  dir,p)) and filenameRx.match(p) if 'thumbnail' not in p]:
>  file, ext = os.path.splitext(picture)
>  im = Image.open (picture)
>  im.thumbnail(size, Image.ANTIALIAS)
>  im.save(file + ".thumbnail" + ext)
>
> jpg = re.compile(".*\.(jpg|jpeg)", re.IGNORECASE)
> thumbnailer(".", jpg)
>
> Thanks for the help!


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: I have a chance to do somting diffrent way not Python ?!

2007-04-28 Thread oscartheduck
Hi anders,

try looking here first:

http://docs.python.org/lib/module-os.html
Also:

http://sourceforge.net/projects/pywin32/


On Apr 28, 10:27 am, anders <[EMAIL PROTECTED]> wrote:
> Hi!
> On my work we have a lot off diffrent server to make software for
> diffrent os
> from Windows, OS/X to Linux Solaris
>
> Everyting is scripted with shell, but Windows  has batchfiles witch is
> very
> limited, compared to cshell etc.
>
> So my boss has told me that it okej if i want to make somting better
> och smooter,
> for the windows machine.
>
> First i looked a Ruby but didn't like it to interacting with the os,
> nothinh wrong
> but my eyes moved to Python becurse syntax is clean and every server
> (not windows) has Python as base installed, and in the future i could
> then move
> the script to run on all servers.
>
> So basicly i like advice about interac with os, compiler etc.
> Nice webblinks to read more
> and do/don't things.
>
> best regards
> Anders


-- 
http://mail.python.org/mailman/listinfo/python-list


capturing system exit status

2007-04-01 Thread oscartheduck
Hi folks,

in a program I'm writing I have several commands I pass to the unix OS
underneath the code.

I want to perform error checking to make sure that the OS commands'
exit gracefully, but I'm not seeing a simple python module to do this.
The closest I can see is system(), as detailed here:
http://www.python.org/doc/2.1.3/lib/os-process.html, but I can't
formulate a way to use it.

What I want is a simple if statement such that:

if ExitStatusIsBad:
  sys.exit()
else:
  go on to next code chunk

-- 
http://mail.python.org/mailman/listinfo/python-list


try... except SyntaxError: unexpected EOF while parsing

2007-04-04 Thread oscartheduck
I have a small script for doing some ssh stuff for me. I could have
written it as shell script, but wanted to improve my python skills
some.

RIght now, I'm not catching a syntax error as I'd like to.

Here's my code:

#!/usr/bin/env python
import sys
import os

port = input("Please enter a port to connect on. If you're unsure or
just want the default of port 2024 just hit enter  --  ")


try:
  if port > 65535:
print "That's not a valid port number, sorry. Between 0 and 65535
is cool."
sys.exit()
  else:
cmd = 'su root -c "/usr/sbin/sshd -p %s"' % port
except SyntaxError:
  cmd = 'su root -c "/usr/sbin/sshd -p 2024;exit"'

os.system(cmd)


I'm under the impression that the except should catch the syntax error
and execute the "default" command, but hitting enter at the input
value doesn't lead to that. Any ideas what's going wrong?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: try... except SyntaxError: unexpected EOF while parsing

2007-04-04 Thread oscartheduck
Wow. Thanks, everyone, for the responses. It helps a lot having such a
well informed and helpful resource.



-- 
http://mail.python.org/mailman/listinfo/python-list


newbie: windows xp scripting

2006-05-21 Thread oscartheduck
I've been having trouble with the following style of script. It's
simple stuff, I know, but it's stumping me:

import os

dirfrom = 'C:\\test'
dirto = 'C:\\test1\\'

copy_command = 'copy "%s" "%s"' % (dirfrom, dirto)

if os.system(copy_command) == 0:
print "yay"
else:
print "boo"


What's going wrong is with the %s substitution part. I've tried more
complex scipts and kept running against an error where my script just
wouldn't work. So I stripped back and back and back to this point,
where I'm just trying to run a simple copy command. But for some
reason, it fails.

If I don't use the %s substitution but use the full pathnames
explicitly in the copy command, it works fine. So that's where my issue
seems to be. Help!

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Does anybody know how to install PythonMagick?

2006-05-21 Thread oscartheduck
What distribution are you using?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: newbie: windows xp scripting

2006-05-21 Thread oscartheduck
It wasn't, but after seeing your success I discovered what was wrong.
My destination directory didn't exist, and for some reason windows
wasn't automatically creating it to dump the files in.

I could fix this with a nested if statement, but it "feels" like
windows should be creating this folder automatically if it doesn't
exist.

Oh well, thanks Steve! I wouldn't have performed the next step of
manunally creating the directory if you hadn't posted that the script
worked!

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: newbie: windows xp scripting

2006-05-21 Thread oscartheduck
It wasn't, but after seeing your success I discovered what was wrong.
My destination directory didn't exist, and for some reason windows
wasn't automatically creating it to dump the files in.

I could fix this with a nested if statement, but it "feels" like
windows should be creating this folder automatically if it doesn't
exist.

Oh well, thanks Steve! I wouldn't have performed the next step of
manunally creating the directory if you hadn't posted that the script
worked!

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: newbie: windows xp scripting

2006-05-21 Thread oscartheduck
For completeness' sake, this is the new script I came up with:

import os

dirfrom = 'C:\\test'
dirto = 'C:\\test1\\'
makedir = 'mkdir "%s"' % dirto
copy_command = 'copy "%s" "%s"' % (dirfrom, dirto)

if os.system(copy_command) == 0:
print "yay"
else:
if os.system(makedir) == 0:
if os.system(copy_command) == 0:
print 'yay!'
else:
print 'WTF mate?'


Is there a more efficient way of doing this?

-- 
http://mail.python.org/mailman/listinfo/python-list