Python Forum

Full Version: problem in using input command
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
(Oct-19-2023, 06:05 AM)akbarza Wrote: [ -> ]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

no reply yet?
Use / instead of \ . Windows doesn't care, and there is no chance that \U will be interpreted as the start of a unicode character.

If you ran this code in interactive python you get a better error message.
Output:
>>> address="C:\Users\akbar\Desktop\techcruch.csv" File "<stdin>", line 1 address="C:\Users\akbar\Desktop\techcruch.csv" ^ SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
You can escape out of the escape sequence by using \\
Output:
>>> address="C:\\Users\akbar\Desktop\techcruch.csv" >>> address 'C:\\Users\x07kbar\\Desktop\techcruch.csv' >>> print(address) C:\Userskbar\Desktop echcruch.csv
Oops! \t is a tab escape sequence. May as well escape out of all the backslashes
Output:
>>> address="C:\\Users\\akbar\\Desktop\\techcruch.csv" >>> print(address) C:\Users\akbar\Desktop\techcruch.csv
Or you could just use /.

More info on escape sequences here.

https://docs.python.org/3/reference/lexi...-sequences
akbarza Wrote:FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\akbar\\Desktop\\techcruch.csv'
The problem is that you do not have file named techcruch.csv on Desktop.
Python never lies👀 about this.
akbarza Wrote:I downloaded it on my desktop, so its address on my PC is: C:\Users\akbar\Desktop\techcruch.csv
It goes in Downloads folder if not specify or move the file to Desktop.

So it work if i put file with that name on my desktop.
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}")

address = r"C:\Users\tom\Desktop\techcruch.csv"
y(address)
Output:
Row count is 65
Also noteworthy that techcruch.csv appears to be a typo.

/regards