1

Python query.

I want to take a copy of a file, called randomfile.dat, and add a timestamp to the end of the copied file.

However, I want to keep the original file too. So in my current directory (no moving files) I would end up with: randomfile.dat randomfile.dat.201711241923 (or whatever the timestamp format is..)

Can someone advise? Anything I have tried causes me to lose the original file.

DaveMac001
  • 155
  • 3
  • 13
  • 1
    It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (console output, tracebacks, etc.). The more detail you provide, the more answers you are likely to receive. Check the [FAQ] and [ask]. – MooingRawr Nov 24 '17 at 19:36
  • How hard is it to google it? If you're feeling lazy, keep in mind it's actually less effort to google than ask a question. – cs95 Nov 24 '17 at 19:45

3 Answers3

1

When you open the file, you can specify how you want to open it with "r", "w", or "a". "a" will append to the file (r - read, w - write).

So:

with open("randomfile.dat", "a") as file:
    file.write("some timestamp")

Or, if you'd like to preserve this original and make a copy, then you need to open this file, copy it, and then open a new file and write to a new file

# empty list to store contents from reading file
file_contents = []
# open file you wish to read
with open('randomfile.dat', 'r') as file:
    for line in file:
        file_contents.append(line)

# open new file to be written to
with open('newfile.txt', 'w') as newfile:
    for element in file_contents:
        newfile.write(element)
    newfile.write("some timestamp")

Any line feeds (\n) will be preserved by the reader and it essentially reads the file line by line. Then you write, line by line, to a new file. After the loop ends, add the timestamp so it writes to the very bottom of the file.


Edit: Just realized OP wanted to do something slightly different. This would still work but you'd need to open the new file with the timestamp appended:

import datetime
datestring = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
with open('newfile' + datestring + '.txt', 'w') as newfile:
    for element in file_contents:
    newfile.write(element)

But as others mention, you might be better off with a module for this.

cchoe1
  • 325
  • 2
  • 15
0
from shutil import copy
from time import time
fn = 'random.dat'
copy(fn, fn+'.'+str(time()))
anki
  • 765
  • 5
  • 14
0

How about this?

$ ls

$ touch randomfile.dat

$ ls
randomfile.dat

$ python
[...]
>>> import time
>>> src_filename = 'randomfile.dat'
>>> dst_filename = src_filename + time.strftime('.%Y%m%d%H%M')

>>> import shutil
>>> shutil.copy(src_filename, dst_filename)
'randomfile.dat.201711241929'
>>> [Ctrl+D]

$ ls
randomfile.dat
randomfile.dat.201711241929
creativecoding
  • 247
  • 2
  • 9