Page 1 of 1

Simple script to replace spaces w/dots for movies category?

Posted: April 25th, 2012, 3:21 pm
by freewayaxe
I am just looking for a script that will rename the folders of all completed downloads in the "movies" category, replacing any spaces in the folder name with dots. Appreciate if someone could help with this, thanks :)

Re: Simple script to replace spaces w/dots for movies catego

Posted: July 4th, 2012, 1:33 pm
by NoTolerance
You can try this script if you'd like.

FYI, I just have it renaming the folder - it doesn't touch the files. If you'd like this changed, let me know.

Re: Simple script to replace spaces w/dots for movies catego

Posted: July 4th, 2012, 6:17 pm
by freewayaxe
Thanks for your help! However, I'm on linux :(

Re: Simple script to replace spaces w/dots for movies catego

Posted: July 4th, 2012, 6:28 pm
by NoTolerance
Oh, okay. That should be relatively simple, too. I'll see if I can knock something out when I'm near a computer again.

Re: Simple script to replace spaces w/dots for movies catego

Posted: July 4th, 2012, 6:36 pm
by freewayaxe
Thanks so much chief! I'm so terrible with scripting, your help is greatly appreciated :)

Re: Simple script to replace spaces w/dots for movies catego

Posted: July 5th, 2012, 3:36 am
by sander
A python script you can run to replace spaces in directory names with dots.
Developed and tested on Linux.

Code: Select all

import os

for path, dirnames, filenames in os.walk('.'):
	for dirname in dirnames:
		if dirname.find(' ') > -1 :
			# Yes, a space in the directory name, so replace it:
			newname = dirname.replace(' ','.')
			print "Rename" , dirname, "   ", newname
			os.rename(dirname, newname)

Re: Simple script to replace spaces w/dots for movies catego

Posted: July 6th, 2012, 4:52 pm
by freewayaxe
Thanks! So I have my scripts in /usr/share/sabnzbd/scripts, do I just make a file like spacestodots.py, and paste that in there? Then have that script run on movies?

Re: Simple script to replace spaces w/dots for movies catego

Posted: July 6th, 2012, 9:42 pm
by sander
Just run it from the command line. Change the '.' to the correct directory.

If that works, you can procreed to call it from SABnzbd

Re: Simple script to replace spaces w/dots for movies catego

Posted: July 8th, 2012, 4:17 pm
by freewayaxe
Okay, I have the script like this at the moment:

Code: Select all

> cat spacestodots.py 
import os

for path, dirnames, filenames in os.walk('/media/stuff/movies/Get\ the\ Gringo\ 720p\ BluRay\ x264\ DTS\ HDChina'):
   for dirname in dirnames:
      if dirname.find(' ') > -1 :
         # Yes, a space in the directory name, so replace it:
         newname = dirname.replace(' ','.')
         print "Rename" , dirname, "   ", newname
         os.rename(dirname, newname)
I run it from command line and get this:

Code: Select all

> python3 spacestodots.py 
  File "spacestodots.py", line 8
    print "Rename" , dirname, "   ", newname
                 ^
SyntaxError: invalid syntax
If I run it with python2 or python2.x, there's no errors but it also doesn't do anything (movie directory remains as is with spaces).

Re: Simple script to replace spaces w/dots for movies catego

Posted: July 8th, 2012, 4:40 pm
by sander
Well, it's for python 2, not for python 3.

Change the directory to work on to '/media/stuff/', not including the movie name.

Re: Simple script to replace spaces w/dots for movies catego

Posted: July 8th, 2012, 5:04 pm
by freewayaxe
Okay I moved one movie into another folder to test the script with. The movie folder is inside /media/stuff/burning/, which is what I changed the path to in the script.

This is what I got:

Code: Select all

> python2 spacestodots.py
Rename Get the Gringo 720p BluRay x264 DTS HDChina     Get.the.Gringo.720p.BluRay.x264.DTS.HDChina
Traceback (most recent call last):
  File "spacestodots.py", line 9, in <module>
    os.rename(dirname, newname)
OSError: [Errno 2] No such file or directory

Re: Simple script to replace spaces w/dots for movies catego

Posted: July 8th, 2012, 5:21 pm
by sander
OK, bug. Change to:

Code: Select all

import os

# workdir = '/home/sander/kul-rename'
workdir = '/home/sander/Downloads/even twee'         #### CHANGE this line to your movie directory

for path, dirnames, filenames in os.walk(workdir):
	print "path:", path

	for dirname in dirnames:
		if dirname.find(' ') > -1 :
			# Yes, a space in the directory name, so replace it:
			newname = dirname.replace(' ','.')
			fulldirname = path + '/' + dirname
			fullnewname = path + '/' + newname
			print "Rename" , fulldirname, "   ", fullnewname
			os.rename(fulldirname, fullnewname)
That works for me

Re: Simple script to replace spaces w/dots for movies catego

Posted: July 8th, 2012, 6:04 pm
by freewayaxe

Code: Select all

> python2 spacestodots.py
path: /media/stuff/burning
Rename /media/stuff/burning/Get the Gringo 720p BluRay x264 DTS HDChina     /media/stuff/burning/Get.the.Gringo.720p.BluRay.x264.DTS.HDChina
Awesome! Thanks a lot mate, I really appreciate your help, I hope anyone else who needs this script can benefit :)