# How can I add a date to a literal file path in Python

**URL:** <https://forum.inductiveautomation.com/t/how-can-i-add-a-date-to-a-literal-file-path-in-python/55995>\
**Category:** Ignition\
**Tags:** ignition81\
**Created:** [February 10, 2022, 6:59pm UTC](https://forum.inductiveautomation.com/t/how-can-i-add-a-date-to-a-literal-file-path-in-python/55995 "2022-02-10T18:59:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rusty](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/rusty/32/19859_2.png) [@Rusty](https://forum.inductiveautomation.com/u/Rusty)\
**Post date:** [February 10, 2022, 6:59pm UTC](https://forum.inductiveautomation.com/t/how-can-i-add-a-date-to-a-literal-file-path-in-python/55995/1 "2022-02-10T18:59:54Z")

</div>

Hello,

I’m trying to add a script to a button to export a table to csv when the button is clicked. The filename should be a timestamp. I’ve added the following to a button script:

```auto
# Create a variable that references our Power Table. You could modify this part
# of the example to point to a different component in the window.
component = event.source.parent.getComponent('Table')
timestamp = system.date.now()
# Use system.dataset.toCSV to turn the dataset into a CSV string
csv = system.dataset.toCSV(component.data)

filepath = "C:\myExports\"  
#I've also tried "C:\\myExports\\" this has no change, if I use r"C:\myExports\" it generates an error
timestampSTR = str(timestamp)
filetype = ".csv"
filestring = filepath+timestampSTR+filetype

# Write to local file system. Note the "r" character right before the directory path.
# This denotes a raw string literal, meaning we ignore escape sequences (like the "/") r"C:\myExports\myExport.csv"
system.file.writeFile(filestring, csv)

```

I’m getting an error since the file path is showing up with double slashes instead of single.I know that you can use “r” before the string to make it literal, but how would I use it in this case?

Thanks,

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [February 10, 2022, 7:10pm UTC](https://forum.inductiveautomation.com/t/how-can-i-add-a-date-to-a-literal-file-path-in-python/55995/2 "2022-02-10T19:10:02Z")

</div>

I’d say it’s an error in your date format, though use of forward slashes is more ‘pythonic’.

```python
timestamp = system.date.format(system.date.now(), 'YYYY-MM-dd_HH_mm_ss')

filepath = 'C:/myExports/{}.csv'.format(timestamp)

print filepath

```

```nohighlight
>>> 
C:/myExports/2022-02-10_14_09_28.csv
>>> 

```

---

<div class="post-metadata">

**Author:** ![Rusty](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/rusty/32/19859_2.png) [@Rusty](https://forum.inductiveautomation.com/u/Rusty)\
**Post date:** [February 10, 2022, 7:14pm UTC](https://forum.inductiveautomation.com/t/how-can-i-add-a-date-to-a-literal-file-path-in-python/55995/3 "2022-02-10T19:14:38Z")

</div>

> [@JordanCClark](#):
>
> ```python
> timestamp = system.date.format(system.date.now(), 'YYYY-MM-dd_HH_mm_ss')
> 
> filepath = 'C:/myExports/{}.csv'.format(timestamp)
> 
> ```

Thank you, That worked!

```python
# Create a variable that references our Power Table. You could modify this part
# of the example to point to a different component in the window.
component = event.source.parent.getComponent('Table')

# Use system.dataset.toCSV to turn the dataset into a CSV string
csv = system.dataset.toCSV(component.data)

timestamp = system.date.format(system.date.now(), 'YYYY-MM-dd_HH_mm_ss')

filepath = 'C:/myExports/{}.csv'.format(timestamp)

# Write to local file system. Note the "r" character right before the directory path.
# This denotes a raw string literal, meaning we ignore escape sequences (like the "/") r"C:\myExports\myExport.csv"
system.file.writeFile(filepath, csv)

```

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [February 10, 2022, 7:17pm UTC](https://forum.inductiveautomation.com/t/how-can-i-add-a-date-to-a-literal-file-path-in-python/55995/4 "2022-02-10T19:17:24Z")

</div>

Glad it worked for you. It’s because colons are not allowed in file names.  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/3/f/3faf35a171b84d43b970a8bbf33b89474a9057e8.png)

---

<div class="post-metadata">

**Author:** ![KathyApplebaum](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kathyapplebaum/32/7261_2.png) [@KathyApplebaum](https://forum.inductiveautomation.com/u/KathyApplebaum)\
**Post date:** [February 10, 2022, 10:39pm UTC](https://forum.inductiveautomation.com/t/how-can-i-add-a-date-to-a-literal-file-path-in-python/55995/5 "2022-02-10T22:39:42Z")

</div>

> [@JordanCClark](#):
>
> `'YYYY-MM-dd_HH_mm_ss'`

Caution: YYYY will give you a "weekyear", which may not be what you want on the first or last week of the year. yyyy will give you the calendar year, which is probably the intention.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [February 11, 2022, 11:53am UTC](https://forum.inductiveautomation.com/t/how-can-i-add-a-date-to-a-literal-file-path-in-python/55995/6 "2022-02-11T11:53:53Z")

</div>

Force of habit as that’s almost always my use case. 😆
