I've written a program using Python 3.10.5 in Visual Studio to read in a block of text from a file and format it as a list. Pretty simple, saves the list to the clipboard so that it can be pasted straight away. Except, when it encounters certain characters in the block of text, errors appear:
- a colon (:) causes the program to treat the following lines like a command. The command cannot be understood, so an error occurs.
- a less than symbol (<) causes the program to look for a file, declare the file cannot be found, and throw an error.
- a more than symbol (>) causes the program to open a new, blank text file, named using the characters that followed the > symbol, and paste the remaining block of text into it.
Why is this happening? And how can I make it stop happening without manually specifying which characters must be ignored or removed from the text before parsing? Code attached below.
import os
def convert_syn_to_list_clip():
out_list = []
with open('syn_list.txt', 'r+', encoding = 'utf-8') as f1:
for line in f1.readlines():
out_list.append(line.rstrip('\n'))
if len(out_list) == 0:
print("EMPTY LIST !!!")
f1.truncate(0)
return str(out_list)
def write_to_clipboard(text):
command = 'echo | set /p nul=' + text.strip() + '| clip'
os.system(command)
write_to_clipboard(convert_syn_to_list_clip())
print("\n\nList in clipboard.\n\n")