1

I want give prefix for file, but i have errors, can u help me?

 import shutil, datetime
 v = datetime.datetime.now()
 x = v.strftime('%Y.%m.%d.')
 shutil.copyfile('C:\\users\\v.korolev\\desktop\\dev\\python\\zxc.txt', x+'C:\\users\\v.korolev\\desktop\\dev\\python\\old\\zxc.txt')
newbody13
  • 21
  • 1
  • 4
  • 1
    You are appending the `x` as prefix to absolute path which makes no sense, You need to append `x` to `zxc` in the file path I guess, for that you must use `os.split()` to split the path and then select the last element, then modify it the way you want, and reconstruct the path using `os.join()` – ZdaR Feb 03 '17 at 08:17
  • *but i have errors* You should always include the error you are getting when you ask a question. – Grisha Levit Feb 03 '17 at 08:21
  • Traceback (most recent call last): File "C:\Users\v.korolev\Desktop\dev\python\copy\copy25.py", line 4, in shutil.copyfile('C:\\users\\v.korolev\\desktop\\dev\\python\\zxc.txt', x+'C:\\users\\v.korolev\\desktop\\dev\\python\\old\\zxc.txt') File "C:\Python27\lib\shutil.py", line 83, in copyfile with open(dst, 'wb') as fdst: IOError: [Errno 22] invalid mode ('wb') or filename: '2017.02.03.C:\\users\\v.korolev\\desktop\\dev\\python\\old\\zxc.txt' – newbody13 Feb 03 '17 at 08:24
  • @ZdaR Can u help me with this code. I understand you, but i can't realize this. – newbody13 Feb 03 '17 at 08:42
  • Possible dupe of : http://stackoverflow.com/questions/18950668/os-path-split-changing-file-name-with-out-compromising-the-path – ZdaR Feb 03 '17 at 08:53

1 Answers1

1
import shutil, datetime, os
v = datetime.datetime.now()
x = v.strftime('%Y.%m.%d.')
file = "C:\\users\\v.korolev\\desktop\\dev\\python\\zxc.txt"
shutil.copyfile(file, "C:\\users\\v.korolev\\desktop\\dev\\python\\old\\%szxc.txt" % x) 

I realize it! Thanks you for help

newbody13
  • 21
  • 1
  • 4