Python Forum

Full Version: problem in entering address of a file in input
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi
i read https://realpython.com/introduction-to-p...enerators/
there is a file as techcruch.csv in the above address.
I downloaded it on my desktop, so its address on my PC is: C:\Users\akbar\Desktop\techcruch.csv
in the below code(IDLE):
address=r"C:\Users\akbar\Desktop\techcruch.csv"
def csv_reader(file_name):
    for row in open(file_name, "r"):
        yield row
def y(address):
    csv_gen = csv_reader(address)
    row_count = 0
    for row in csv_gen:
       row_count += 1
    print(f"Row count is {row_count}")


y(address)
the below error is displayed:
Error:
Traceback (most recent call last): File "<pyshell#27>", line 1, in <module> y(address) File "<pyshell#26>", line 5, in y for row in csv_gen: File "<pyshell#2>", line 2, in csv_reader for row in open(file_name, "r"): FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\akbar\\Desktop\\techcruch.csv'
I also did the below tests and you can see what idle showed to me:

address="C:\Users\akbar\Desktop\techcruch.csv"
Error:
SyntaxError: incomplete input
address=r"C:\Users\akbar\Desktop\techcruch.csv"
address
Output:
'C:\\Users\\akbar\\Desktop\\techcruch.csv'
address=f"C:\Users\akbar\Desktop\techcruch.csv"
Error:
SyntaxError: incomplete input
address=r"C:\Users\akbar\Desktop\techcruch.csv"
y(address)
Error:
Traceback (most recent call last): File "<pyshell#35>", line 1, in <module> y(address) File "<pyshell#26>", line 5, in y for row in csv_gen: File "<pyshell#2>", line 2, in csv_reader for row in open(file_name, "r"): FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\akbar\\Desktop\\techcruch.csv'
so what can I do to input the address of the techcruch.csv file to the y function?
thanks