-2

I have this code:

import sys

cities = 'me, myself, eye'

company_types = 'WHAT, WHO'

with open("result.txt", 'w') as fid:
    for city in cities:
        for company_type in company_types:
            fid.write(company_type.strip() + " in " + city.strip())
            fid.write("\r\n")

I get this output:

<built-in method write of _io.TextIOWrapper object at 0x0000019E4C3017D0>

What is wrong with the code? How do I fix it?

petezurich
  • 9,280
  • 9
  • 43
  • 57
  • Welcome back to Stack Overflow. Please read [ask] and note well that this is **not a discussion forum**. We do not want conversational language in posts; we want **directly** asked questions - the importance of the question to you is [not our concern](https://meta.stackoverflow.com/questions/326569). Please also read the [formatting help](https://stackoverflow.com/help/formatting) and make sure you understand how to post code with proper formatting. – Karl Knechtel Oct 07 '22 at 05:13
  • 1
    How about `cities = ["me", "myself", "eye"]`, `company_types = ["WHAT", "WHO"]`? And your (current) code couldn't possibly generate that output. – CristiFati Oct 07 '22 at 05:13
  • As for the question: the code shown does not cause the result shown. Please read [mre] and make sure (by trying it yourself) that someone else can **copy and paste** the code shown and **directly** see the **exact** problem described. – Karl Knechtel Oct 07 '22 at 05:15
  • If the question is "why does the file contain wrong lines that look like `W in m`?", that is because using a `for` loop on a string loops over **the individual letters**. If you want to treat the string as words, please see https://stackoverflow.com/questions/743806. Also please consider **following a Python tutorial** from start to finish, to learn the fundamentals; this is introductory material that any proper text should cover near the beginning. – Karl Knechtel Oct 07 '22 at 05:17

3 Answers3

1

Please convert cities and company_types to lists or any iterable type.

cities = ['me', 'myself', 'eye']

company_types = ['WHAT', 'WHO']

with open("/full/path/to/file/result.txt", 'w') as fid:
    for city in cities:
        for company_type in company_types:
            fid.write(company_type.strip() + " in " + city.strip())
            fid.write("\r\n")

Try to use full paths to ensure that file is created in exact place you wanted, instead of Python current path.

0
import sys

#you are defining a string instead of a list, Try this.
cities = ['me', 'myself', 'eye']
company_types = ['WHAT', 'WHO']

with open("result.txt", 'w') as fid:
    for city in cities:
        for company_type in company_types:
            fid.write(company_type.strip() + " in " + city.strip())
            fid.write("\r\n")
N. Desai
  • 25
  • 7
  • Code-only answers are discouraged on Stack Overflow. Please provide a description of how your answer solves the problem, provide references where appropriate and show how your solution differs from the other answers provided. – DaveL17 Oct 10 '22 at 14:26
-2

Code is ok, I've just check it. But check you file, may be it is already presend and need to set access to it. Or use full path to file. For example, if I need make new file near my script I use:

os.path.join(os.path.dirname(os.path.realpath(__file__)), "result.txt")