-1

I went across similar questions of renaming files in a directory using python.

I have these files in a directory which I want to rename:

    -statistical_analysis_with_r
    -statistical_pattern_recognition_3rd_edition

to

    -Statistical Analysis With R
    -Statistical Pattern Recognition 3rd Edition

For this I wrote this script in windows:

def naming(so):
    import re
    w=re.split('[ _]+',so)
    r=[]
    for i in w:
        r.append(i.capitalize())
    print(' '.join(r))  



import os
for c in os.listdir(os.getcwd()):
    if c.endswith(".pdf"):
        os.rename(c,naming(c))
print(os.listdir(os.getcwd()))

But I am getting this error:

Statistical Analysis With R.pdf
Traceback (most recent call last):

  File "<ipython-input-79-d7f645d6d3e5>", line 4, in <module>
    os.rename(c,naming(c))

TypeError: rename: can't specify None for path argument

Can anybody help what's going on?

And any help doing same thing using R (www.r-project.org)?

Loads of thanks in advance.

Suman Khanal
  • 3,079
  • 1
  • 17
  • 29

3 Answers3

2

Your naming function doesn't return the name, it prints out the name and returns None. That's why you're getting an error about None.

Change this: print(' '.join(r))

To this: return ' '.join(r)

ezig
  • 1,219
  • 1
  • 10
  • 15
1

You're mixing up your languages. In R, the function will return the last thing referred to, but in Python you have to explicitly say "return ' '.join(r)". Or maybe you're mixing up "print" and "return". Either way, you're not explicitly returning anything in your naming() function so you're getting the None object.

As for your naming() function, you can just do this:

>>> foo = "foo_bar_foobar"
>>> foo.replace('_', ' ').title() # Your whole naming() function, in one line!
'Foo Bar Foobar'

This is much more "Pythonic" because it uses the built-in functions provided by the string class. Since that code is all tested and widely verified (by every other Python programmer) you can be much more confident that, when you encounter a bug, it's NOT in the replace() or title() functions. This is one of the biggest benefits of languages like Python. When you reinvent the wheel, you have to do all your own testing and debugging on your new wheel. Languages like Python, Ruby, etc. give you very reliable wheels.

Dan Cusher
  • 121
  • 5
1

An R solution,

  1. write a small utility function as in here for the capitalization (title-style) chunk

    simpleCap <- function(x) { s <- strsplit(x, " ")[[1]] paste(toupper(substring(s, 1,1)), substring(s, 2), sep="", collapse=" ") }

  2. read the file(s) of interest (for readability)

    files <- dir(pattern = ".pdf$") #".pdf$" as an example

3a. nicely, using the (pipe) "%>%" operator

library(dplyr)

files %>% 
gsub("_", " ", .) %>% 
lapply(simpleCap) %>% 
unlist %>% 
file.rename(from = files, to = .)

3b. or in a less nice version, without the 'pipe'

file.rename(from = files, to = unlist(lapply(gsub("_", " ", files), simpleCap))
Community
  • 1
  • 1
Pasqui
  • 591
  • 4
  • 12