Dylan Lawrence
Dylan Lawrence

Reputation: 1533

Drag and drop on script not behaving properly

So, I made this quick python script that takes a file name and fixes it up for use on linux.

import os
import sys

args = sys.argv

file = args[1].split('\\')
file = file[len(file)-1]

newfile = ''

for char in file:
    if char.isupper():
        newfile += '_' + char.lower()
    elif not char.isalnum() and char != '.':
        newfile += '_'
    else:
        newfile += char

newfile = newfile.lstrip('_')
os.rename(args[1],newfile)

It does its job properly, but if you drag the file on top if it, it won't rename the file. I debugged with some print statements and it does receive the file as an arg if you drag and drop it, but it won't rename it. Any help would be appreciated.

Edit: At request, adding some OS details.

Using Windows 7, SP1 64 bit, Ultimate Edition Using Python 2.7 64 bit

Upvotes: 1

Views: 134

Answers (1)

Ricardo
Ricardo

Reputation: 136

Now is more brief and simple.

import os
import sys

args = sys.argv
path = "\\".join(args[1].split('\\')[:-1])
file_name = args[1].split("\\")[-1]

file_name = file_name.lower()
file_name = file_name.replace(' ','_')

file_name = path+'\\'+file_name
os.rename(args[1],file_name)

Upvotes: 1

Related Questions