-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChapter body.txt
96 lines (74 loc) · 6.06 KB
/
Chapter body.txt
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
User:Write a Python programme according to the following requirements, check the programme after answering and modify the programme if it is wrong Note: 1. For the folder mentioned later, create the folder if it doesn't exist 2. The functions mentioned later are located in api.py in the current directory, which needs to be imported and the corresponding functions called. Functions include input and output functions, save and read files through this module to achieve the function. Function parameters for the previous steps to create the corresponding name of the string, all parameters for the string form 3. save and read the file using utf-8 encoding, the file does not exist then create a file, save the file to cover the contents of the 4. pay attention to the conversion of the number of strings expressed in the string and the number of numbers represented by an integer, make sure that the input integer type of comparison procedure steps: 1. create an integer seq, the initial value of 1. Read the num.txt in the setting folder in the current directory, and save the number in the file to the integer num 2. The following steps are executed using multi-threaded execution (each thread executes all of the following steps once), the interval between thread creation is 0.1 seconds, and the number of threads reaches 64 after the pause in the creation of threads to create the variables created by the use of local variables. Each time you create a thread, add 1 to the value of seq, and repeat until seq=num. 3. Create the integer part, with an initial value of 1. Name the folder in the chapter folder in the current directory with a number, and name the txt file inside the folder with a number, and read the txt file with filename=part from the folder with filename=seq, save it to the outline_current string, in part<5 read the txt file with filename=part+1, save it to outline_next string 4. when part=1, call context_first(outline_next,outline_current) function; when part>1 and part <5, call context_mid (last, online_next, online_current) function; when part = 5, call context_last (last, online_current) function. After each call, the value of part is increased by 1, and the call is repeated until part=5. The contents returned by the above three functions are saved to the last string, and saved to the txt file with name=part in the folder with name=seq in the context folder in the current directory.
Assistant:The following is a Python programme written according to your requirements:
```python
import os
import threading
import time
import api
# Functions to create folders
def create_folder_if_not_exists(folder_path):
if not os.path.exists(folder_path):
os.makedirs(folder_path)
# Read the contents of the file
def read_file(file_path):
if os.path.exists(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
return file.read()
return ""
# Save the contents of the file
def save_file(file_path, content):
with open(file_path, 'w', encoding='utf-8') as file:
file.write(content)
# Functions to execute the threads
def thread_function(seq):
part = 1
last = ""
while part <= 5:
chapter_folder = os.path.join("chapter", str(seq))
create_folder_if_not_exists(chapter_folder)
outline_current_file = os.path.join(chapter_folder, f"{part}.txt")
outline_current = read_file(outline_current_file)
outline_next = ""
if part < 5:
outline_next_file = os.path.join(chapter_folder, f"{part + 1}.txt")
outline_next = read_file(outline_next_file)
if part == 1:
last = api.context_first(outline_next, outline_current)
elif 1 < part < 5:
last = api.context_mid(last, outline_next, outline_current)
elif part == 5:
last = api.context_last(last, outline_current)
context_folder = os.path.join("context", str(seq))
create_folder_if_not_exists(context_folder)
context_file = os.path.join(context_folder, f"{part}.txt")
save_file(context_file, last)
part += 1
# main function
def main():
setting_folder = "setting"
create_folder_if_not_exists(setting_folder)
num_file = os.path.join(setting_folder, "num.txt")
num_str = read_file(num_file)
num = int(num_str) if num_str.isdigit() else 1
seq = 1
threads = []
while seq <= num:
thread = threading.Thread(target=thread_function, args=(seq,))
threads.append(thread)
thread.start()
seq += 1
time.sleep(0.1)
if len(threads) >= 64:
for thread in threads:
thread.join()
threads = []
for thread in threads:
thread.join()
if __name__ == "__main__":
main()
```
### Procedure checks and modifications
1. **Folder creation**: The `create_folder_if_not_exists` function ensures that a folder is created if it does not exist. 2. 2. **File reading and saving**: `read_file` and `save_file` functions are used to read and save file contents respectively, using `utf-8` encoding. 3. **Thread management**: `thread_function` function defines the workflow of each thread, including reading and saving files, and calling functions in `api.py`. 4. **Main program**: `main` function is responsible for initialising the setup, reading the number in the `num.txt` file and creating threads based on that number.
### Change points
1. **Folder path splicing**: make sure the folder path is spliced correctly, use `os.path.join`. 2. 2. **Thread count control**: when the number of created threads reaches 64, wait for all threads to finish before continuing to create new threads. 3. **File reading and saving**: Ensure that files are read and saved using `utf-8` encoding. 4. **Number conversion**: after reading the contents of the `num.txt` file, make sure it is converted to an integer type.
This program should meet your needs, please let me know if you have any problems or need further modifications.