+ 1
Python3 Problem with files
data2.txt: Danang Adit Rei data.txt: Danang(Fullname) Adit(Fullname) Rei(Fullname) Index.py: data = open('data.txt','r') user = str(input("Name:").lower() import dec dec.userx = user if user not in data.read().lower(): import endek dec.py: userx = "?" endek.py: data2 open('data2.txt','r') import dec #code that detect if userx is in data2.txt and if it is found it will save the founded line a variable and then open data.txt and print the line founded
23 ответов
+ 1
# index.py (fixed and commented):
user = str(input("Nama Panjang/Pendek:")).lower()
import dec
dec.userx = user
#data = open('data.txt','r')
data2 = open('data2.txt','r') # you search in list of (short) names rather than of fullname
#if user not in data.read().lower():
if user in data2.read().lower(): # if found rather than not found
import endek
data2.close() # if you don't use 'with' construct, you need to close explicitly the file
# endek.py (fixed and commented):
print('endek.py')
#with open('data2.txt','r') as data2:
with open('data.txt','r') as data: # conversely, you have found user (short) name, you search now corresponding entry in fullnames
import dec
num = 0
#for name in data2: # what's the fuck ?
for name in data.readlines(): # iterate over each lines
#if dec.userx in data2.readl().lower(): # readl doesn't exists ^^
if dec.userx in name.lower():
#print("halo,",name[num])
print("halo,",name)
else:
num += 1
+ 1
# index.py
user = str(input("Nama Panjang/Pendek:")).lower()
import dec
dec.userx = user.lower()
data = open('data.txt','r')
data2 = open('data2.txt','r')
if user in data2.read().lower():
# ok, here you know that user name is in short names list, so we call the
# module to find the corresponding long name...
import endek
else:
# but what's the fuck if user name not in short names list?
# did you expect that user name is in data while not in data2?
num = 0 # anyway, you don't need a counter/index here...
for names in data.readlines():
"""
if user name match long name case sensitive, that's the correct way
to do... but we need probably at least to compare case insensitive:
"""
# if user in names:
if user in names.lower():
print("Halo,",names)
else:
num += 1
# close your opened files before exit:
data.close()
data2.close()
###
# endek.py
data = open('data.txt','r')
#data2 = open('data.txt','r')
data2 = open('data2.txt','r')
num = 0
import dec
for name in data2.readlines():
#if dec.userx in name:
"""
we must compare case insensitive to have result...
"""
if dec.userx in name.lower():
# print("Halo,",name)
"""
well, user name found in short names list: now, use the num
counter/index to access the long names list...
"""
print("Halo,",data.readlines()[num])
else:
num += 1
# close your opened files before exit:
data.close()
data2.close()
+ 1
Ok: the problem occurs when you get more than one match for an user name ^^
The file handler (returned by the open() function) would need to be "rewinded" for "readlines" more than once...
Fix:
(at start of endek.py)
#data = open('data.txt','r')
with open('data.txt','r') as txt: data = txt.readlines()
(at end of endek.py)
#data.close()
(just remove the line: the file hanfdler is implicity closed thanks to the "with" construction...)
That's all for the actual problem, but I would prevent a future one...
At start of index.py, after the line:
dec.userx = user.lower()
... insert this one:
user = dec.userx
... that will store the lowercased version of user name (else the lowercased version is only stored in dec.userx ^^)
0
I don't know if your code will run as you expect, but you needs to at least correct the line:
user = str(input("Name:").lower()
to:
user = str(input("Name:")).lower()
(missing closing parenthesis)
0
visph Uhh i mistyped but the code work because i write 4% of the code
0
If you want accurate help, you need to provide accurate source(s) and explanation of what you expect and what you got (plus the error message if there's one)...
Also, I didn't understood your "the code works because i write 4% of the code"??? (how could it works if it's not complete? or how could you be sure it works if you are not the original author ans have only changed 4%? You only need to change one char in a code to break it ^^)
0
i expect my code to ask for user input and then store that input in a file called rec.txt write mode and then if the user input is not in data.txt then the main program import endek.py and then endek.py open a file called rec.txt and then store the content in a variable and then endek.py open a file called data2.txt as read and then search the same string in the variable that contain user input and then print the open a file called data.txt as read and the print the line that the name found in data2.txt ex:
data.txt:
Rei(fullname)
data2.txt:
Rei
as you can see if user input is rei its found string Rei in data2.txt and then print the same line in data.txt
ex:
Name:Rei
Hello, Rei(fullname)
0
I was meaning: what's is the "problem with files" you encounter exactly? ... and what do you expect instead?
(imagine: I walk to a mechanic, and I ask for help by saying: I have a problem with my car... he needs to know what kind of problem, in wich context, and finally he needs to see my car ^^)
0
https://code.sololearn.com/crhM5XS9RhKo/?ref=app
https://code.sololearn.com/c0b2FmP5A2qg/?ref=app
https://code.sololearn.com/cXSMi3JaK0qC/?ref=app
here is the code
data.txt:
Abdullah Danang Ahmad Hadi
Aditya Zandra Ega Fauzi
Alvreius Dante Christanto
Andhira Alga Pramesta
Anggun Putri Lestari
Anugerah Ikhtifazhuddin Rachman
Bagus Raharjo Samiaji
Bintang Putra Pratama
Catherine Sifra Majesty N
Felin Arizka Fitri
Haidar Akmal Faiz Rianto
Hamzah Dwi Prakoso
Iftya Aura Permana
Isti Amalia Efendi
Kevin Candrawinata
Keyla Revania Kurniawan
Keysa Sizi Fricilia
Lala Aulia Rahmadani
Moch Adi Irfansya
Mochamad Irsad Fais Novahudin
Moch Ferinur Irawan
M Rasya Putra Prasetyo
Much Fachri
Muhammad Aditya Pratama
Muhammad Fadhil Alfarisy
Muhammad Iqbalul Muharrom
Nabila Aisyah Cahyani
Nafisa Septia Ramadhani
Najwa Keysa Salsabila
Noura Az Zahra
Novita Artika Sari
Phalosa Garneta Husna
Rafirno Ray Tristan
Rhino Revaldo
Sera Agus Mandasari
Silvia Larasati
Sofia Ayu Pratiwi
Titan Mei Setiawan
Ulul Fahmi
0
data2.txt:
Danang
Adit Z
Rei Rey
Andhira
Anggun
Nanu
Bagus
Bintang
Cathy
Felin
Faiz
Hamzah
Pipit
Amel
Kevin
Keyla
Sisil
Lala
Adi
Noval
Ferinur
Rasya
Fachri
Adit P
Fadhil
Iqbal
Nabila
Sasa
Najwa
Noura Naura
Novita
Neta Netta
Tristan
Aldo
Sera
Silvia
Sofi
Titan
Fahmi
0
# your counter ("num") is useless, and spliting your program into module is weird...
# you could get same result with once (shorter) file:
with open('data.txt') as data: fullnames = data.readlines()
with open('data2.txt') as data: names = data.readlines()
user = str(input("Nama Panjang/Pendek:")).lower()
for index, name in enumerate(names):
if name.strip().lower()==user: print("halo,",fullnames[index])
# and anyway, there are corner cases about what you should think and take in account ;)
0
visph THX😁
0
visph Uhh some of my friends name are not from their full name.... Can you uhh help me again?..
0
What do you mean by "are not from their full name"?
0
My friend Anugerah his short name is Nanu
and others....
thats why i put data2.txt
if look at line 6 the name is different from data.txt
0
Oh, yeah... I've found:
1) my bad... in index.py you should replace all 'data' by 'data2' (or reciprocally, but I wanted to follow your logic to name the variable as the file)
2) my bad too... I have not done enough tests, and the code was working as is for the only name I've tested (sofi)... but testing if user name is in fullname isn't enough (when short name is NOT in fullname)
Well, you could try to fix it yourself (and request for help if you stuck): the idea would be to iterate over the lines of data2.txt in index.py, use a counter and store it instead of the user name, then you could access the corresponding line with its number... (so, it's almost what I do in the shorter code I suggested, without the weird stuff of modules ;P)
0
visph Yeah Trouble on for loops
for names in data.readlines():
if names is user:
print(names)
break
else:
pass
output:
Nama Panjang/Pendek: Alvreius Dante Christanto
[Program Finished]
conclusion:
It doesn't work
no error encounter
0
yes, because "names" is never "user": when you use readlines() to get the file content as an array of lines, the '\n' ending line chars are kept, so you're compared 'abc' with 'abc\n' (except for the last line of the file if not empty)...
So, you should either append '\n' to the user input (but you will fail with the last user name in the list), or better 'clean' the line...
At least 2 solutions again: strip() the readlines() values ("names" in your code snippet): remove the spaces at start and end of a string:
print(repr(' abc '.strip())) # "abc"
... or read the whole file and split the lines:
data = data_file.split('\n')
... where the separator is dropped from the resulting string array, unlike the readlines() method ;)
0
Yeah new problems I found out that Only Rei is known so if i input rei it will print the full name but not on everyone
*Code Updated
0
Problems:
Nama Panjang/Pendek:adit
Halo, Aditya Zandra Ega Fauzi
Traceback (most recent call last):
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in <module>
start(fakepyfile,mainpyfile)
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start
exec(open(mainpyfile).read(), __main__.__dict__)
File "<string>", line 9, in <module>
File "/storage/emulated/0/FileManipulate/lib/Dat/Process/DataManager/Bin/New/endek.py", line 10, in <module>
print("Halo,",data.readlines()[num])
IndexError: list index out of range
[Program finished]
should i remove one of them?[adit p is a exchanged student]