-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopy_file.py
More file actions
99 lines (82 loc) · 2.93 KB
/
copy_file.py
File metadata and controls
99 lines (82 loc) · 2.93 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#copy_file.py
###############################################################
# Copy file
#
# Author: Adam A. Koch (aakoch)
# Date: 2018-04-13
# Copyright (c) 2018 Adam A. Koch
# This work is licensed under the MIT license.
###############################################################
import uos, machine, log
from led_functions import *
# ############################################
# Copies *text* files
# ############################################
def copy_file(inFilename, outFilename):
with open(inFilename, "rt") as f1:
with open(outFilename, "wt") as f2:
for line in f1:
f2.write(line)
f1.close()
f2.close()
def file_exists(filename):
for file in uos.ilistdir():
if (file[0] == filename):
return True
return False
def read_first_line(filename):
if file_exists:
file = open(filename, "rt")
line = file.readline()
file.close()
return line
else:
raise ArgumentError(filename + " doesn't exist")
def read_first_line_or_blank(filename):
try:
line = read_first_line(filename)
return line
except RuntimeError:
warn("File", filename, "doesn't exist")
return ""
# ########################################################
# Swaps what is currently in main.py into the original
# file name as determined by the first line of the
# script.
# 'filename' is the name of the file to copy into main.py
# ########################################################
def swap_main(filename):
first_line_in_main = read_first_line("main.py")
# assert that the first line starts with "#" and ends with ".py"
if (first_line_in_main and first_line_in_main[0] == "#" and \
len(first_line_in_main) > 3 and first_line_in_main.strip()[-3:] == ".py"):
main_original_filename = first_line_in_main[1:-3]
if file_exists(filename):
pass
elif file_exists(filename + ".py"):
filename += ".py"
if file_exists(filename):
copy_file("main.py", main_original_filename)
copy_file(filename, "main.py")
else:
raise RuntimeError(filename, "not found")
else:
raise RuntimeError("Could not determine what file to move main.py to. first_line_in_main=" + first_line_in_main)
green_led.on()
pyb.delay(1000)
swap_main("copy_file2.py")
machine.reset()
#print(uos.listdir())
#line = read_first_line("main.py")
#print("the first line is", line)
## to trim a string in python, use strip()
#if (line.strip() == "#copy_file.py" and file_exists("pulse_led.py")):
#print("main file is copy_file.py")
#copy_file("main.py", "copy_file.py")
#copy_file("pulse_led.py", "main.py")
#machine.reset()
#elif not file_exists("pulse_led.py"):
#print("pulse_led.py file does not exist")
#elif line.strip() != "#copy_file.py":
#print("the first line in main.py is not this script")
#print(uos.listdir())