Python Forum
problem in using input command
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem in using input command
#1
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
Larz60+ write Oct-18-2023, 10:04 AM:
Please don't create multiple threads on the same subject. You can add additional posts to the original thread.
Reply
#2
(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?
Reply
#3
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 likes this post
Reply
#4
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
akbarza likes this post
Reply
#5
Also noteworthy that techcruch.csv appears to be a typo.

/regards
snippsat and akbarza like this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem with print command in super() akbarza 5 610 Feb-01-2024, 12:25 PM
Last Post: deanhystad
  problem in entering address of a file in input akbarza 0 661 Oct-18-2023, 08:16 AM
Last Post: akbarza
  Facing Problem while opening a file through command prompt vlearner 4 1,928 Jan-30-2022, 08:10 AM
Last Post: snippsat
  Problem with input after function luilong 10 4,121 Dec-04-2021, 12:16 AM
Last Post: luilong
  How to input & output parameters from command line argument shantanu97 1 2,587 Apr-13-2021, 02:12 PM
Last Post: Larz60+
  Problem restricting user input in my rock paper scissors game ashergreen 6 4,642 Mar-25-2021, 03:54 AM
Last Post: deanhystad
  single input infinite output problem Chase91 2 1,962 Sep-23-2020, 10:01 PM
Last Post: Chase91
  Problem with the input marios 4 2,077 May-03-2020, 01:01 PM
Last Post: marios
  Taking Multiple Command Line Argument Input bwdu 6 4,073 Mar-29-2020, 05:52 PM
Last Post: buran
  command line input (arg parse) and data exchange Simba 7 4,341 Dec-06-2019, 11:58 PM
Last Post: Simba

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020