-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
86 changed files
with
22,314 additions
and
5,194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# URS 900 Batch Process | ||
|
||
以批次作業方式,執行 a500, a510, a520 Python 程式。 | ||
|
||
程式執行規格: | ||
|
||
```powershell | ||
a900_Batch_Process.py a520_製作注音網頁.py -i output xlsx | ||
``` | ||
1. 參數 1:Python 程式檔名; | ||
` | ||
2. 參數 2: `子目錄` 路徑; | ||
|
||
3. 參數 3: 副檔名; | ||
|
||
程式執行之初,將依據 `參數 3` 指定之副檔名,自 `參數 2` 指定之子目錄,將同副檔名的所有檔案,製成 Files List。生成 Files List 的過程中, | ||
程式會參酌 exculude_list 清單,將清單中列舉之檔案排除掉。 | ||
|
||
Files List 中的各個檔案,只需放入檔案名稱,不需附上子目錄路徑: | ||
|
||
`【正確格式】:` | ||
|
||
```python | ||
files = [ | ||
'【河洛話注音】前赤壁賦.xlsx', | ||
... | ||
] | ||
``` | ||
|
||
`【錯誤格式】:` | ||
|
||
```python | ||
files = [ | ||
'output\【河洛話注音】前赤壁賦.xlsx', | ||
... | ||
] | ||
``` | ||
|
||
最後,程式將依 Files List ,自 Command Line 啟動 `參數1` 指定之 Python 程式碼,並傳入一個`輸入檔案`,如: | ||
|
||
```powershell | ||
python a520_製作注音網頁.py -i 【河洛話注音】前赤壁賦.xlsx | ||
``` | ||
|
||
|
||
|
||
|
||
你可以使用 Python 的 `argparse` 模組來處理命令行參數。這個模組可以讓你定義需要的參數,並且在使用者沒有提供參數時,給予預設值或者顯示錯誤訊息。以下是一個範例: | ||
|
||
```python | ||
import argparse | ||
|
||
def get_cmd_input(): | ||
parser = argparse.ArgumentParser(description='Process some files.') | ||
parser.add_argument('-i', '--input', default='default_input.txt', help='Input file name') | ||
parser.add_argument('-o', '--output', default='default_output.txt', help='Output file name') | ||
args = parser.parse_args() | ||
|
||
return { | ||
"input": args.input, | ||
"output": args.output, | ||
} | ||
``` | ||
|
||
在這個範例中,`-i` 和 `-o` 是命令行參數的縮寫,`--input` 和 `--output` 是完整的參數名稱。`default` 參數是在使用者沒有提供參數時的預設值,`help` 參數是當使用者輸入 `-h` 或 `--help` 時顯示的說明訊息。 | ||
|
||
使用者可以透過以下方式來提供參數: | ||
|
||
``` | ||
python your_script.py -i input.txt -o output.txt | ||
``` | ||
|
||
或者 | ||
|
||
``` | ||
python your_script.py --input input.txt --output output.txt | ||
``` | ||
|
||
如果使用者沒有提供 `-i` 或 `-o` 參數,則會使用 `default` 參數指定的預設值。 | ||
|
||
```python | ||
import argparse | ||
|
||
def get_cmd_input(): | ||
parser = argparse.ArgumentParser(description='Process some files.') | ||
parser.add_argument('-i', '--input', default='default_input.txt', help='Input file name') | ||
parser.add_argument('-o', '--output', default='default_output.txt', help='Output file name') | ||
args = parser.parse_args() | ||
|
||
return { | ||
"input": args.input, | ||
"output": args.output, | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# URS 910 Batch Process by TODO List | ||
|
||
依據 TODO List 檔案內容,以批次作業方式,執行 a500, a510, a520 Python 程式。 | ||
|
||
程式執行規格: | ||
|
||
```powershell | ||
a910_Batch_Process_By_Todo_List.py a520_製作注音網頁.py | ||
``` | ||
參數 1:Python 程式檔名 | ||
|
||
|
||
程式依 toto_list.txt 檔案內容,自 Command Line 啟動 `參數1` 指定之 Python 程式碼,並傳入一個`輸入檔案`,以如下指令格式執行: | ||
|
||
```powershell | ||
python a520_製作注音網頁.py -i 【河洛話注音】前赤壁賦.xlsx | ||
``` | ||
todo_list.txt 檔案格式: | ||
|
||
```python | ||
【河洛話注音】回鄉偶書.賀知章.xlsx | ||
【河洛話注音】姜伯約歸降孔明,武鄉侯罵死王朗.xlsx | ||
【河洛話注音】岳陽樓記.xlsx | ||
【河洛話注音】岳陽樓記.范仲淹.xlsx | ||
【河洛話注音】易經。同人卦.xlsx | ||
【河洛話注音】晁錯論.蘇軾.xlsx | ||
【河洛話注音】歸去來辭.陶淵明.xlsx | ||
【河洛話注音】滕王閣序.王勃.xlsx | ||
【河洛話注音】證人得獎感言.xlsx | ||
【河洛話注音】《水龍吟·登建康賞心亭》.xlsx | ||
``` | ||
|
||
程式處理步驟: | ||
|
||
1. 匯入 `sys` 和 `subprocess` 模組。 | ||
2. 從命令列參數取得 Python 程式的名稱。 | ||
3. 開啟 `todo_list.txt` 檔案並讀取每一行。 | ||
4. 對每一行,使用 `subprocess` 執行指定的 Python 程式,並將該行作為輸入檔案參數。 | ||
|
||
以下是 Python 程式碼: | ||
|
||
```python | ||
import sys | ||
import subprocess | ||
|
||
# 從命令列參數取得 Python 程式的名稱 | ||
python_program = sys.argv[1] | ||
|
||
# 開啟 todo_list.txt 檔案並讀取每一行 | ||
with open('todo_list.txt', 'r', encoding='utf-8') as file: | ||
for line in file: | ||
# 移除換行符號 | ||
input_file = line.strip() | ||
|
||
# 使用 subprocess 執行指定的 Python 程式,並將該行作為輸入檔案參數 | ||
subprocess.run(['python', python_program, '-i', input_file]) | ||
``` | ||
|
||
請注意,這個程式碼假設 `todo_list.txt` 檔案和 Python 程式都在同一個目錄下。如果不是,您可能需要提供完整的路徑。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# URS 920 製作目錄檔案清單 | ||
|
||
依據程式輸入之參數,為指定之`目錄路徑`,製作檔案清單。 | ||
|
||
程式執行規格: | ||
|
||
```powershell | ||
a920_製作目錄檔案清單.py [參數1] [參數2] [參數3] [參數4] | ||
``` | ||
- 參數1:欲產生之`目錄檔案清單`名稱; | ||
- 參數2:指定之目錄路徑; | ||
- 參數3:副檔案名稱 (亦指檔案類型) | ||
- 參數4:除外清單檔案 (此參數可略去不輸入) | ||
|
||
程式之執行,是為了要產生一個純文字檔型態的`檔案清單`。檔案清單之`檔案名稱`,依據`參數1`指定。而檔案清單之製作過程,會依據`參數2`至`目錄路徑`去撈檔案,而檔案之撈取,只取`參數3`之指之同類型檔案。 | ||
|
||
若執行執行時有輸入`參數4`:除外清單時,則代表生成`檔案生成`時得有排外考量,只要是出現在`除外清單檔案`內的檔案,就不要放入`目錄檔案清單`檔案之中。 | ||
|
||
舉例: | ||
|
||
```powershell | ||
python a920_製作目錄檔案清單.py todo_list.txt output xlsx exclude_files.txt | ||
``` | ||
|
||
以上指令表:程式要從 `output` 子目錄去撈取所有`副檔名`為 `xlsx` 的 Excel 檔案。但因為使用者有輸入`參數4:除外清單檔案`,所以,在除外清單檔案條列的所有檔案,均需自`目錄檔案清單`中排除掉。 | ||
|
||
假設 `exclude_files.txt` 檔案的內容如下: | ||
|
||
```powershell | ||
env.xlsx | ||
env_osX.xlsx | ||
Piau-Tus-Im.xlsx | ||
``` | ||
|
||
那就代表,以上三個存在`output`子目錄中的 Excel 檔案,應自`目錄檔案清單`排除掉。 | ||
|
||
參數1 之 `目錄檔案清單` : `todo_list.txt` ,其檔案內容舉例如下: | ||
|
||
```python | ||
【河洛話注音】回鄉偶書.賀知章.xlsx | ||
【河洛話注音】姜伯約歸降孔明,武鄉侯罵死王朗.xlsx | ||
【河洛話注音】岳陽樓記.xlsx | ||
【河洛話注音】岳陽樓記.范仲淹.xlsx | ||
【河洛話注音】易經。同人卦.xlsx | ||
【河洛話注音】晁錯論.蘇軾.xlsx | ||
【河洛話注音】歸去來辭.陶淵明.xlsx | ||
【河洛話注音】滕王閣序.王勃.xlsx | ||
【河洛話注音】證人得獎感言.xlsx | ||
【河洛話注音】《水龍吟·登建康賞心亭》.xlsx | ||
``` | ||
|
||
程式作業流程: | ||
|
||
使用 Python 的 `os` 和 `sys` 模組來完成這個任務。以下是程式的執行步驟: | ||
|
||
1. 匯入 `os` 和 `sys` 模組。 | ||
2. 從命令列參數取得 `目錄檔案清單` 名稱、目錄路徑、副檔名和除外清單檔案。 | ||
3. 使用 `os.listdir()` 函數獲取目錄中的所有檔案。 | ||
4. 過濾出與指定副檔名相符的檔案。 | ||
5. 如果提供了除外清單檔案,則讀取該檔案並從檔案清單中排除在除外清單中的檔案。 | ||
6. 將最終的檔案清單寫入到 `目錄檔案清單` 檔案中。 | ||
|
||
以下是 Python 程式碼: | ||
|
||
```python | ||
import os | ||
import sys | ||
|
||
# 從命令列參數取得目錄檔案清單名稱、目錄路徑、副檔名和除外清單檔案 | ||
list_file_name = sys.argv[1] | ||
directory_path = sys.argv[2] | ||
file_extension = sys.argv[3] | ||
exclude_file = sys.argv[4] if len(sys.argv) > 4 else None | ||
|
||
# 獲取目錄中的所有檔案 | ||
all_files = os.listdir(directory_path) | ||
|
||
# 過濾出與指定副檔名相符的檔案 | ||
filtered_files = [file for file in all_files if file.endswith(file_extension)] | ||
|
||
# 如果提供了除外清單檔案,則讀取該檔案並從檔案清單中排除在除外清單中的檔案 | ||
if exclude_file: | ||
with open(exclude_file, 'r', encoding='utf-8') as file: | ||
exclude_files = file.read().splitlines() | ||
filtered_files = [file for file in filtered_files if file] | ||
``` |
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import subprocess | ||
import sys | ||
|
||
# 從命令列參數取得 Python 程式的名稱 | ||
python_program = sys.argv[1] | ||
|
||
# 獲取當前 Python 環境的路徑 | ||
python_path = sys.executable | ||
|
||
# 開啟 todo_list.txt 檔案並讀取每一行 | ||
with open('todo_list.txt', 'r', encoding='utf-8') as file: | ||
for line in file: | ||
# 移除換行符號 | ||
input_file = line.strip() | ||
|
||
# 使用 subprocess 執行指定的 Python 程式,並將該行作為輸入檔案參數 | ||
subprocess.run([python_path, python_program, '-i', input_file]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import os | ||
import sys | ||
|
||
# 從命令列參數取得目錄檔案清單名稱、目錄路徑、副檔名和除外清單檔案 | ||
list_file_name = sys.argv[1] | ||
directory_path = sys.argv[2] | ||
file_extension = sys.argv[3] | ||
exclude_file = sys.argv[4] if len(sys.argv) > 4 else None | ||
|
||
# 獲取目錄中的所有檔案 | ||
all_files = os.listdir(directory_path) | ||
|
||
# 過濾出與指定副檔名相符的檔案 | ||
filtered_files = [file for file in all_files if file.endswith(file_extension)] | ||
|
||
# 如果提供了除外清單檔案,則讀取該檔案並從檔案清單中排除在除外清單中的檔案 | ||
if exclude_file: | ||
with open(exclude_file, 'r', encoding='utf-8') as file: | ||
exclude_files = file.read().splitlines() | ||
filtered_files = [file for file in filtered_files if file not in exclude_files] | ||
|
||
# 將最終的檔案清單寫入到目錄檔案清單檔案中 | ||
with open(list_file_name, 'w', encoding='utf-8') as file: | ||
for file_name in filtered_files: | ||
file.write(file_name + '\n') |
Oops, something went wrong.