0

I have a program that concatenates French words separated by an asterisk in a text. As I want this program to be used by different users, I want to insert a line in the program asking the user to enter the path of the text file or simply enter the name of the text…How to do that? Just using function “input”? I have no idea…Is there an elegant way to ask that to the user to run the program? The program is below:

import nltk
from nltk.tokenize import word_tokenize
import re



with open ('text-test.txt') as tx:
   words = word_tokenize(tx.read().lower())

with open ('Fr-dictionary.txt') as fr:
    dic = word_tokenize(fr.read().lower())

l=[ ]
errors=[ ]
out_file=open("newtext.txt","w")

for n,word in enumerate (words):
    l.append(word)
    if word == "*":
        exp = words[n-1] + words[n+1]
        print("\nconcatenation error:", exp)

        if exp in dic:

            l.append(exp)
            l.append("$")

            errors.append(words[n-1])

            errors.append(words[n+1])
        else:
           continue

for i, w in enumerate(l):
    if w == "*":
        l.remove(l[i-1])
    else:
        continue

for i, w in enumerate(l):
    if w == "$":
        l.remove(l[i+1])
    else:
        continue


text=' '.join(l)
print('\n\n',text)
e=len(errors)

print('\n',e/2,'WORDS CONCATENATED IN TEXT',errors)


user=input('\nREMOVE * AND $ FROM TEXT? Type "Y" for yes or "N" for 
no:')


for x in l:
    if user=='Y' and x=='*':
        l.remove(x)
    elif user=='Y' and x=='$':
        l.remove(x)
    else:
        continue


final_text=' '.join(l)

print('\n\n', final_text)


user2=input('\nWrite text to a file? Type "Y" for yes or "N" for no:')

if user2 =='Y':
    out_file.write(final_text)
    out_file.close()
    print('\nText named "newtext.txt" written to a file')
Natalia Resende
  • 185
  • 1
  • 1
  • 15
  • Possible duplicate of [User input and command line arguments](https://stackoverflow.com/questions/70797/user-input-and-command-line-arguments) – user24343 Mar 17 '19 at 12:40

2 Answers2

0

You can do it any way you like, but having your users write out a full path to a file is tedious and error prone. What you could do is have a "watch folder". It's a folder that your script already knows about, maybe even in the same folder as your script.

A small example:

import os
import sys

# This prints the folder where the script is run.
script_directory = os.path.dirname(sys.argv[0])
print(script_directory)

# This is the folder we want to keep track off
our_watched_folder = f'{script_directory}/watch_folder'
print(our_watched_folder)

# Let's see if a user dropped a new file in our folder
print("Files in watch folder")
for file in os.listdir(our_watched_folder):
    print(file)

Output:

C:/your_script_folder/
C:/your_script_folder/watch_folder
Files in watch folder
a_new_text_file.txt
some_old_textfile1.txt
some_old_textfile2.txt
NoSplitSherlock
  • 605
  • 4
  • 19
0
from pathlib import Path
data_folder = Path(str(input("type the path you would like to use")))
file_to_open = data_folder / str(input("insert the file you would like to use with its extension"))
f = open(file_to_open)

If you don't want to use the full path and just use a local file located in the script's location, you just need to ask the user for its name and open it with f = open(filename) directly.

Note: If you are wondering why there's a / in file_to_open instead of a string concatenation + this explains why.

fuomag9
  • 138
  • 1
  • 12