Rename .sav files with SPSS/Python?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Rename .sav files with SPSS/Python?

Ruben Geert van den Berg
Dear all,
 
Is there any way (with SPSS or Pyton) to rename a number of .sav files in a single folder as 'file_1', 'file_2' etcetera?
 
TIA and have a nice weekend!
 
Ruben van den Berg



 





What can you do with the new Windows Live? Find out
Reply | Threaded
Open this post in threaded view
|

Re: Rename .sav files with SPSS/Python?

Albert-Jan Roskam
Hi Ruben!

import glob, os.path
path = "d:/temp"
savs = glob.glob(os.path.join(path, "*.sav"))
if savs:
  for k, sav in enumerate(sorted(savs)):
    os.rename(sav, os.path.join(os.path.dirname(sav), "file_"+str(k+1)+".sav"))

If you want to rename the files recursively, use os.walk instead of globbing. The files are sorted by filename, then renamed.

Cheers!!
Albert-Jan


--- On Fri, 7/31/09, Ruben van den Berg <[hidden email]> wrote:

> From: Ruben van den Berg <[hidden email]>
> Subject: Rename .sav files with SPSS/Python?
> To: [hidden email]
> Date: Friday, July 31, 2009, 10:00 AM
>
>
>
> #yiv1715241684 .hmmessage P
> {
> margin:0px;padding:0px;}
> #yiv1715241684 {
> font-size:10pt;font-family:Verdana;}
>
>
>
> Dear all,
>
>
>
> Is there any way (with SPSS or Pyton) to rename a
> number of .sav files in a single folder as 'file_1',
> 'file_2' etcetera?
>
>
>
> TIA and have a nice weekend!
>
>
>
> Ruben van den Berg
>
>
>
>
>
>
>
>
> What can you do with the new Windows Live? Find
> out
>

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD