0

I'm having a bit of an issue trying to print the output of my code into a .txt file.

I am looking to print the results of my code to a .txt file, which I can then use the number printed as a variable for how many lines from the top of the .txt file to read from, separating that bit of text into it's own .txt file.

I have attached a link to a small diagram to explain this better 1

I've been researching for a while and can't seem to find it! Does anyone have any ideas on how to do this?

My code is the following:

from itertools import permutations
import string


# Enter new brand names to following lists

brand_names = "8x8, AmazonAWS, Checkpoint, Cisco, Citrix, Commvault, Dell, Dell_EMC, Google, Google_Cloud, HPE, Hyland, IBM, IBM_Cloud, Microsoft, Microsoft_Azure, NetApp, Oracle, Pure_Storage, SAP, Thompson_Reuters, Veritas, VMware, Aerohive, Aramark, Bloomberg, BMC, Box, CompuCom, Cybera, Extreme, FireEye, GE, Globoforce, GPSI, Infor, Lux_Research, MetTel, Oracle_Cloud, PAR_Invista, Puppet, Rackspace, Ruckus, Salesforce, SonicWall, SPI, Splunk, Stratix, Supermicro, Tenable, Ultipro, US_Bank, Veeam, VIP"

for group in permutations(['8x8', 'AmazonAWS', 'Checkpoint', 'Cisco', 'Citrix', 'Commvault', 'Dell', 'Dell_EMC', 'Google', 'Google_Cloud', 'HPE', 'Hyland', 'IBM', 'IBM_Cloud', 'Microsoft', 'Microsoft_Azure', 'NetApp', 'Oracle', 'Pure_Storage', 'SAP', 'Thompson_Reuters', 'Veritas', 'VMware', 'Aerohive', 'Aramark', 'Bloomberg', 'BMC', 'Box', 'CompuCom', 'Cybera', 'Extreme', 'FireEye', 'GE', 'Globoforce', 'GPSI', 'Infor', 'Lux Research', 'MetTel', 'Oracle_Cloud', 'PAR_Invista', 'Puppet', 'Rackspace', 'Ruckus', 'Salesforce', 'SonicWall', 'SPI', 'Splunk', 'Stratix', 'Supermicro', 'Tenable', 'Ultipro', 'US Bank', 'Veeam', 'VIP'], 3):
    print('_'.join(group)) 
print
print

# Python3 code to demonstrate 
# to count words in string 
# using regex (findall()) 
import re 


# printing original string 
print ("The original string is : ")
print
print("'" + brand_names + "'") 


# using regex (findall()) 
# to count words in string 
res = len(re.findall(r'\w+', brand_names)) 

print
print
print

# printing result 
print ("The number of brands are : " + str(res)) 

print
print
N = res
import sys
sys.stdout = open("file.txt", "w+")
print (group)
Owen Murray
  • 135
  • 1
  • 8
  • 1
    Possible duplicate of [Correct way to write line to file?](https://stackoverflow.com/questions/6159900/correct-way-to-write-line-to-file) – ymbirtt Oct 04 '19 at 09:26
  • 2
    Hi Owen, welcome to Stack Exchange! I'm pretty sure this has already been answered on another question. The part where you redefine `sys.stdout` is a little dangerous, and chances are you want to do something along the lines of `with open('file.txt', 'w+') as f: f.write(group)`. The question I linked should tell you what you need. – ymbirtt Oct 04 '19 at 09:28
  • 1
    Hey Owen, hope this come of use to you: 1. empty print statements won't do anything. If you are looking to insert new line, add '\n'. 2. When you open a file without specified full path, it will create the file in the current working directory(where the script executes). 3. Answer found here: https://stackoverflow.com/questions/4675728/redirect-stdout-to-a-file-in-python on second response of the accepted answer. – Guy Tabak Oct 04 '19 at 09:32
  • Hey Guys, thanks for getting back to me so quick, I know about writing to a text file, however I am struggling to figure out which variable I am using to put into the text. I thought it was 'print('_'.join(group))' but it's not printing anything. How would I go around turning my permutated data into a variable? Also, I am very very VERY new to Python, so this all a learning experience for myself. – Owen Murray Oct 04 '19 at 12:39

2 Answers2

0

You'd use <filename>.write rather than print.

Using stdout would be dangerous and might affect parts of your code which you hadn't considered - I'd recommend removing that part of your code completely.

Corsaka
  • 364
  • 3
  • 15
0

You can also use the > operator

python trial.py > trial.txt

Ishan Joshi
  • 487
  • 3
  • 7