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

Come up with a useful post-processing script? Share it here!
Post Reply
freewayaxe
Newbie
Newbie
Posts: 10
Joined: March 20th, 2012, 5:57 pm

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

Post 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 :)
NoTolerance
Full Member
Full Member
Posts: 127
Joined: June 27th, 2012, 9:55 am

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

Post 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.
freewayaxe
Newbie
Newbie
Posts: 10
Joined: March 20th, 2012, 5:57 pm

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

Post by freewayaxe »

Thanks for your help! However, I'm on linux :(
NoTolerance
Full Member
Full Member
Posts: 127
Joined: June 27th, 2012, 9:55 am

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

Post 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.
freewayaxe
Newbie
Newbie
Posts: 10
Joined: March 20th, 2012, 5:57 pm

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

Post by freewayaxe »

Thanks so much chief! I'm so terrible with scripting, your help is greatly appreciated :)
User avatar
sander
Release Testers
Release Testers
Posts: 9264
Joined: January 22nd, 2008, 2:22 pm

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

Post 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)
freewayaxe
Newbie
Newbie
Posts: 10
Joined: March 20th, 2012, 5:57 pm

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

Post 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?
User avatar
sander
Release Testers
Release Testers
Posts: 9264
Joined: January 22nd, 2008, 2:22 pm

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

Post 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
freewayaxe
Newbie
Newbie
Posts: 10
Joined: March 20th, 2012, 5:57 pm

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

Post 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).
User avatar
sander
Release Testers
Release Testers
Posts: 9264
Joined: January 22nd, 2008, 2:22 pm

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

Post 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.
freewayaxe
Newbie
Newbie
Posts: 10
Joined: March 20th, 2012, 5:57 pm

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

Post 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
User avatar
sander
Release Testers
Release Testers
Posts: 9264
Joined: January 22nd, 2008, 2:22 pm

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

Post 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
freewayaxe
Newbie
Newbie
Posts: 10
Joined: March 20th, 2012, 5:57 pm

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

Post 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 :)
Post Reply