-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMove Files.py
79 lines (55 loc) · 2.43 KB
/
Move Files.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# Databricks notebook source
# MAGIC %md
# MAGIC ## Move files from temporary filestore to my mnt area
# COMMAND ----------
# DBTITLE 0,Move files from temporary filestore to my mnt area
tmp_area = 'dbfs:/FileStore/cris_temp'
dbutils.fs.cp(tmp_area, 'dbfs:/mnt/lab/unrestricted/[email protected]/hmrc-api/', recurse = True)
# COMMAND ----------
# MAGIC %md
# MAGIC ## Once above is complete use below to empty temp area
# COMMAND ----------
dbutils.fs.rm(tmp_area, recurse = True)
# COMMAND ----------
# MAGIC %md
# MAGIC ## Download file from dbfs to local computer
# COMMAND ----------
# Python function to download file
def download_link(filepath):
# NB filepath must be in the format dbfs:/ not /dbfs/
# Get filename
filename = filepath[filepath.rfind("/"):]
# Move file to FileStore
dbutils.fs.cp(filepath, f"dbfs:/FileStore/{filename}")
# Construct download url
url = f"https://{spark.conf.get('spark.databricks.workspaceUrl')}/files/{filename}?o={spark.conf.get('spark.databricks.clusterUsageTags.orgId')}"
# Return html snippet
return f"<a href={url} target='_blank'>Download file: {filename}</a>"
# COMMAND ----------
# MAGIC %md
# MAGIC ## Other Databricks Utils
# COMMAND ----------
# DBTITLE 1,This will copy a file from one folder to another
dbutils.fs.cp('dbfs:/FileStore/1x_rstudio.sh', 'dbfs:/databricks/scripts/1x_rstudio.sh')
# COMMAND ----------
# DBTITLE 1,This will copy a folders contents to another folder
dbutils.fs.cp(
'dbfs:/mnt/lab/unrestricted/[email protected]/hmrc-api/data',
'dbfs:/mnt/lab/unrestricted/[email protected]/hmrc-api/data_raw',
True
)
# COMMAND ----------
# DBTITLE 1,This will move a file from one folder to another
dbutils.fs.mv(
'dbfs:/mnt/lab/unrestricted/[email protected]/hmrc-api/CN_CODES_MASTER_TABLE.csv',
'dbfs:/mnt/lab/unrestricted/[email protected]/hmrc-api/lookup_tables'
)
# COMMAND ----------
# DBTITLE 1,This will move a folders contents to another folder
dbutils.fs.mv('<pathname>', '<pathdestination>', True)
# COMMAND ----------
# DBTITLE 1,Create a new folder
dbutils.fs.mkdirs('dbfs:/mnt/lab/unrestricted/[email protected]/hmrc-api/data_clean')
# COMMAND ----------
# DBTITLE 1,Delete a file/folder
dbutils.fs.rm('dbfs:/mnt/lab/unrestricted/[email protected]/hmrc-api/data', True)