1

Newbie Python question.

I'm trying to rename files in a directory...

the value of path is

C:\tempdir\1\0cd3a8asdsdfasfasdsgvsdfc1.pdf

while the value newfile is

C:\tempdir\1\newfilename.pdf

origfile = path
newfile = path.split("\\")
newfile = newfile[0]+"\\"+newfile[1]+"\\"+newfile[2]+"\\"+text+".pdf"

os.rename(path, newfile)
print origfile
print newfile

im getting the following error...

os.rename(path, newfile)
WindowsError: [Error 3] The system cannot find the path specified

I know the directory and file are good because i can call os.stats() on it. I have changed to value of newfile to include the new file name only but recieve the same error (after reading the python documentation on rename())

My imported libraries are....

import sys
import os
import string
from os import path
import re
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfpage import PDFPage
from cStringIO import StringIO

I've read some other threads on this topic - pertaining to absolute vs. relative paths. Obviously, my intent is to use absolute paths. My variables are string variable, anotherwords...

origfile = "C:\tempdir\1\0cd3a8asdsdfasfasdsgvsdfc1.pdf"

Is that enough? or am i supposed to be using some other declaration to tell python this is a path?

Skinner
  • 1,461
  • 4
  • 17
  • 27
  • 1
    Are you not escaping the backslashes in `origfile`? – Stephen B Sep 27 '16 at 16:13
  • 1
    In general `parentdir, filename = os.path.split(origfile); newfile = os.path.join(parentdir, text + '.pdf')` is a more robust way of doing things than trying to split and reassemble around slashes by hand. This might help you avoid errors, although I must say I can't actually see an error in what you've done. I guess it would be worth checking there are no slashes in the value of `text`, since you haven't shown us that. – jez Sep 27 '16 at 16:15
  • How ridiculous of me. this analyzes a couple thousand files and renames them based on a string of text it finds at known location. Yes, some (including the very first file) have a "/" in the file name. Should have suspected. However, i love the os.path.split() nugget. Very useful and thanks. – Skinner Sep 27 '16 at 17:23

2 Answers2

1

Can you try the following instead? You might find that renaming is easier while using a different API.

import pathlib
parent = pathlib.Path('C:/') / 'tempdir' / '1'
old = parent / '0cd3a8asdsdfasfasdsgvsdfc1.pdf'
new = parent / 'newfilename.pdf'
old.rename(new)

Using the pathlib module makes working with paths in a cross-platform fashion somewhat simpler.

Noctis Skytower
  • 21,433
  • 16
  • 79
  • 117
0

You should better use ntpath (explained here) to modify only your filename:

>>> filepath = 'C:\\tempdir\\1\\0cd3a8asdsdfasfasdsgvsdfc1.pdf'
>>> dirname, filename = ntpath.dirname(filepath), ntpath.basename(filepath)
>>> dirname
'C:\\tempdir\\1'
>>> filename
'0cd3a8asdsdfasfasdsgvsdfc1.pdf'

Hence, you will probably be able to use rename as follows:

>>> os.rename(filepath, dirname + ntpath.sep + 'newfilename.pdf')

Using ntpath.sep uses the appropriate separator.

Community
  • 1
  • 1
Aif
  • 11,015
  • 1
  • 30
  • 44