rename file with batch command

Asked by Thomas T. Tran on 2017-05-10

I tried to rename an existing file in sikuli using a batch command but get the following error:

TypeError: unsupported operand type(s) for +: 'NoneType' and 'list'
using this code:

import subprocess
from time import gmtime, strftime
now = strftime("%Y-%m-%d_%H:%M:%S", gmtime())
src = "C:\Users\Murph\xxx_yyy_.jpg"
dst = "xxx_yyy_zzz_" + now + "_.jpg"
subprocess.call('ren %s %s'%(src,dst), shell=True)
then I tried a different code and got this error: SyntaxError: ("no viable alternative at input ''%s''",

different code:

import os
os.system(ren '%s','%s' < %(src,dst))
What am I missing?

error code1:
[error] Abgebrochen
[error] Ein Fehler in Zeile 17
[error] Fehlermeldung: Traceback (most recent call last):
File "C:\Users\Murph\AppData\Local\Temp\sikuli-tmp8294784858223312402.py",
line 17, in
subprocess.call('ren %s %s'%(src,dst), shell=True)
File "D:\Program Files (x86)\Sikuli X\sikuli-script.jar\Lib\subprocess.py",
line 456, in call
File "D:\Program Files (x86)\Sikuli X\sikuli-script.jar\Lib\subprocess.py",
line 751, in __init__
File "D:\Program Files (x86)\Sikuli X\sikuli-script.jar\Lib\subprocess.py",
line 1236, in _execute_child
TypeError: unsupported operand type(s) for +: 'NoneType' and 'list'

error code2:
[error] Abgebrochen
[error] Ein Fehler in Zeile 17
[error] Fehlermeldung: Traceback (most recent call last):
File "C:\Users\Murph\AppData\Local\Temp\sikuli-tmp6179560160671230371.py",
line 17, in
os.system('ren %s %s' %(src,dst))
File "D:\Program Files (x86)\Sikuli X\sikuli-script.jar\Lib\subprocess.py",
line 456, in call
File "D:\Program Files (x86)\Sikuli X\sikuli-script.jar\Lib\subprocess.py",
line 751, in __init__
File "D:\Program Files (x86)\Sikuli X\sikuli-script.jar\Lib\subprocess.py",
line 1236, in _execute_child
TypeError: unsupported operand type(s) for +: 'NoneType' and 'list'

Question information

Language:
English Edit question
Status:
Answered
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Last query:
2017-05-10
Last reply:
2017-05-10
RaiMan (raimund-hocke) said : #1

Python Grundwissen ;-)

\ in strings must be either escaped as \\ or it must be a raw String:

either:
src = "C:\\Users\\Murph\\xxx_yyy_.jpg"

or
src = r"C:\Users\Murph\xxx_yyy_.jpg"

BTW: for renaming files, there are Python builtin functions
https://www.tutorialspoint.com/python/os_rename.htm

module os is already imported in Sikuli scripts.

Can you help with this problem?

Provide an answer of your own, or ask Thomas T. Tran for more information if necessary.

To post a message you must log in.