You are not logged in.

#1 2012-07-16 03:01:23

colbert
Member
Registered: 2007-12-16
Posts: 809

Need help with simple python script (renaming and moving folder)

I have this script for use with sabnzbd, what it does is rename all folders in the desired directory, replacing any spaces with dots (example: Movie 2011 HD -> Movie.2011.HD).

#!/usr/bin/env python2

workdir = '/media/stuff/movies'         #### 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)

What I would like it to do is 1. rename only the movie I run the script on (instead of /media/stuff/movies as above, the entire movie folder), and then 2. move the movie into /media/stuff/movies. Or vice versa! (I'd just prefer not to have it run on the entire movie directory).

Appreciate any help!

Offline

#2 2012-07-16 03:14:16

Inxsible
Forum Fellow
From: Chicago
Registered: 2008-06-09
Posts: 9,183

Re: Need help with simple python script (renaming and moving folder)

I just use simple bash function find. This is what I use to move torrents

find ~/downloads/torrents -type d -exec mv {} ~/media/downloads \;

you can probably add a new name in it

and this is what I use to clean up the torrent and remove unnecessary files like .nfo etc.

find ~/downloads/torrents -type f \( -name "*.txt" -o -name "*.nfo" \) -exec rm -rf {} +;

Forum Rules

There's no such thing as a stupid question, but there sure are a lot of inquisitive idiots !

Offline

#3 2012-07-16 03:51:18

colbert
Member
Registered: 2007-12-16
Posts: 809

Re: Need help with simple python script (renaming and moving folder)

Okay, so the moving part is clear, how in bash would I find and replace spaces in the name with dots? I can refer to $1, that's built-in to sabnzbd as the folder of the download. So I want to find all the spaces in $1 and replace with dots, then move $1 over.

Offline

#4 2012-07-16 07:56:41

Darksoul71
Member
Registered: 2010-04-12
Posts: 319

Re: Need help with simple python script (renaming and moving folder)

Just my two cents:
I would not use walkdir but as usual a combination of find and python.

A simplified version of the script could look like this:

#!/usr/bin/python
# -*- coding: utf8 -*-
import os, shutil

# Define target dir
target_dir = "/media/stuff/movies/"

# Loop through all found files
for source_file in os.popen ("find ."):
  # Remove \n
  source_file = source_file[:-1]
  extension = source_file[-3:]
  
  # Only process video files
  if (extension == 'mkv') or (extension == 'avi') or (extension == 'mp4'): 
    target_file = source_file.split('/')
    target = target_file[len(target_file)-1] 
    target = target.replace(' ','.')
    shutil.copy (source_file, targetdir + target)

Of course you might first want to print out instead of really copy files.


My archlinux x86_64 host:
AMD E350 (2x1.6GHz) / 8GB DDR3 RAM / GeForce 9500GT (passive) / Arch running from 16GB USB Stick

Offline

#5 2012-07-16 23:32:03

cfr
Member
From: Cymru
Registered: 2011-11-27
Posts: 7,152

Re: Need help with simple python script (renaming and moving folder)

detox might be useful.


CLI Paste | How To Ask Questions

Arch Linux | x86_64 | GPT | EFI boot | refind | stub loader | systemd | LVM2 on LUKS
Lenovo x270 | Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz | Intel Wireless 8265/8275 | US keyboard w/ Euro | 512G NVMe INTEL SSDPEKKF512G7L

Offline

#6 2012-07-16 23:37:52

colbert
Member
Registered: 2007-12-16
Posts: 809

Re: Need help with simple python script (renaming and moving folder)

Sorry how do I do that? (print out instead of copy).

Offline

#7 2012-07-17 08:04:29

Darksoul71
Member
Registered: 2010-04-12
Posts: 319

Re: Need help with simple python script (renaming and moving folder)

colbert wrote:

Sorry how do I do that? (print out instead of copy).

Sorry, I thought this is obvious !  smile

Replace this line:

shutil.copy (source_file, targetdir + target)

with this line:

print  source_file + ' ' + targetdir + target

BTW: cfr is right ! detox also looks promising for your purpose:
http://linux.die.net/man/1/detox

HTH,
D$

Last edited by Darksoul71 (2012-07-17 08:07:57)


My archlinux x86_64 host:
AMD E350 (2x1.6GHz) / 8GB DDR3 RAM / GeForce 9500GT (passive) / Arch running from 16GB USB Stick

Offline

Board footer

Powered by FluxBB