# Shutil copy taking too much time

**URL:** <https://forum.inductiveautomation.com/t/shutil-copy-taking-too-much-time/93629>\
**Category:** General Discussion\
**Tags:** scripting\
**Created:** [July 23, 2024, 7:33pm UTC](https://forum.inductiveautomation.com/t/shutil-copy-taking-too-much-time/93629 "2024-07-23T19:33:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![selladhurai\_sellappa](https://avatars.discourse-cdn.com/v4/letter/s/6de8d8/32.png) [@selladhurai\_sellappa](https://forum.inductiveautomation.com/u/selladhurai_sellappa)\
**Post date:** [July 23, 2024, 7:33pm UTC](https://forum.inductiveautomation.com/t/shutil-copy-taking-too-much-time/93629/1 "2024-07-23T19:33:19Z")

</div>

Hi,  
I am using the following code to export a CSV file. But it is not copying sometimes and running too long without exit and halt the whole tag change script even i cancelled the running query. It will back to normal only gateway restarts

```python

def CreateRecordCSV(job, serial):
	import os.path
	import datetime
	import shutil
	localPath = 'R:/OracleIgnition/'
	remotePath = 'Z:/'
	timestamp = ''
	fileName = ''
	d = datetime.datetime.today()
	
	_params = {}
	_params['day'] = d.strftime('%d')
	_params['month'] = d.strftime('%b').upper()
	_params['year'] = d.strftime('%Y')
	_params['hour'] = d.strftime('%H')
	_params['minute'] = d.strftime('%M')
	_params['second'] = d.strftime('%S')	
	_params['mili'] = d.strftime('%f')[:3]
	_params['mon'] = d.strftime('%m')
	
	timestamp = _params['day'] + '-' + _params['month'] + '-' + _params['year'] + ' ' + _params['hour'] + ':' + _params['minute'] + ':' + _params['second']
	fileName = 'ignition_' + _params['year'] + _params['mon'] + _params['day'] + _params['hour'] + _params['minute'] + _params['second'] + _params['mili'] 
	
	import csv  
	data = [job, serial, timestamp]
	completeLocal = os.path.join(localPath, fileName+".csv")    
	with open(completeLocal, 'w+') as f:
		writer = csv.writer(f)
		writer.writerow(data)
		
#	Copy to Oracle server
	completeRemote = os.path.join(remotePath, fileName+".csv")	
	shutil.copy(completeLocal, completeRemote)

```

Could you please someone help me

---

<div class="post-metadata">

**Author:** ![zacht](https://avatars.discourse-cdn.com/v4/letter/z/ea5d25/32.png) [@zacht](https://forum.inductiveautomation.com/u/zacht)\
**Post date:** [July 23, 2024, 9:59pm UTC](https://forum.inductiveautomation.com/t/shutil-copy-taking-too-much-time/93629/2 "2024-07-23T21:59:12Z")

</div>

Some general thoughts that might be helpful:

1. You mention using a tag change script. If this is a [tag event script](https://www.docs.inductiveautomation.com/docs/8.1/platform/tags/tag-event-scripts) you certainly shouldn't be initiating long-running actions (like file writes). A better place is in a [gateway event tag change script](https://www.docs.inductiveautomation.com/docs/8.1/platform/scripting/scripting-in-ignition/gateway-event-scripts#tag-change-scripts).
2. If there's more than one way to accomplish a task, you should pick the tools in this order.
  1. `system.*` functions. ([Documentation](https://www.docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions))
  2. Java libraries
  3. Python libraries

  - In particular, I would [strongly] recommend performing your date logic something like this:

```python
now = system.date.now()
timestamp = system.date.format(now, 'dd-MM-yyyy HH:mm:ss')
fileNameDate = system.date.format(now, 'yyyyMMddHHmmssSSS')
fileName = r'ignition_{}.csv'.format(fileNameDate)

```

and your file writes using the `system.file.*` functions.
    - The Jython implementation of the Python standard libraries is sometimes buggy. I wouldn't be surprised if shutil or os.path are running slowly and causing you problems where one of the `system.file.*` functions would perform much better.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [July 23, 2024, 10:03pm UTC](https://forum.inductiveautomation.com/t/shutil-copy-taking-too-much-time/93629/3 "2024-07-23T22:03:34Z")

</div>

> [@selladhurai\_sellappa](#):
>
> ```python
> localPath = 'R:/OracleIgnition/'
> remotePath = 'Z:/'
> 
> ```

If either of these are network drives, latency is going to be insanely variable. `shutil.copy` has no timeout parameter that I can see, so you're easily prone to ~infinite loops if the network drive hiccups or something else happens, even aside from the often buggy Jython standard library @zacht mentioned.

This is also a _crazy_ complicated script to append a single line of text to a file.
