python - Rename a file with a pattern -


i have filename looks

er-log-0.0.1-20150807.194034-8.jar 

the format follows

artifactid-version-timestamp.jar 

i want rename artifactid.jar.

i tried

>>> fname = "er-log-0.0.1-20150807.194034-8.jar" >>> import os >>> os.rename("er-log*", "er-log.jar") traceback (most recent call last):   file "<stdin>", line 1, in <module> oserror: [errno 2] no such file or directory 

the file in current directory though

you need glob if want find files using shell wildcard:

import os glob import glob path = "/path_to_files"  f = glob(os.path.join(path,"er-log*"))[0] os.rename(f, os.path.join(path,"er-log.jar")) 

using "er-log*" os.rename, python looking file called "er-log*".
if run code same directory, don't need join path, os.rename(f, "er-log.jar")


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -