How to redirect a file of data as input and and typing EOF Character (Ctrl+Z - Windows) in Spyder

10 views
Skip to first unread message

Milton Eduardo Peña Aza

unread,
Oct 30, 2019, 1:33:40 PM10/30/19
to spyder
Dear All, 
Good day! My name is Milton an would like to kindly ask the following questions:

1) Would like to input a file of data as input to a test python script wrote in Spyder. However, I do not know how to do it inside Spyder. 

Have managed to do it when running the script in Windows Console (MS-DOS Prompt) by using the command line:


C:\Users\milton> python C:\Users\milton\Desktop\uno.py < C:\Users\milton\Desktop\integers.dat


2) For the same test script, users have the possibility to interactively insert data and, then, terminate the manual input by typing the EOF Character (Ctrl+Z - Windows), 

Again, this can be achieved in Windows Console

C:\Users\milton> python C:\Users\milton\Desktop\uno.py
Type integers, each followed by Enter; or CTRL + D or CTRL + Z to finish
1
2
3
^Z
count
= 3 total= 6 mean= 2.0


Would be very grateful if the group could please provide with some insights on how to do it

Sincerely,
Milton

Milton Eduardo Peña Aza

unread,
Oct 31, 2019, 3:23:30 PM10/31/19
to spyder
Dear All, 

There is a workaround to input the file data, it is by opening\selecting  and reading the file and looping over it as 

file = input ('Enter file name:')
linelist
= [line.rstrip('\n') for line in open(file)]
   
print(linelist)
total
= 0
count
= 0
for line in linelist:
    number
= int(line)
   
print(number)
    total
+= number
    count
+= 1

So far, so good, thus, my question is it is possible to redirect a file of data as input in a similar way as Windows Console does?

Thanks,

Sincerely,

Milton

Reply all
Reply to author
Forward
0 new messages