Python Forum
why it showed that the argument of my function was not defined?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
why it showed that the argument of my function was not defined?
#1
here is my codes,I wrote a function which should be able to read a csv file and parse it into some useful form.

import csv


def load_atlas(filename):
    edited_file = []
    with open('filename', 'r') as csv_file:
        csv_reader = csv.reader(csv_file, delimiter='|')
        for lines in csv_reader:
            edited_file.append(lines)
    return edited_file
load_atlas(ala-acton.txt)
my problem is whenever i passed a filename into this function, it returns an error
Error:
NameError: name 'ala' is not defined
Reply
#2
place it in quotes:
load_atlas('ala-acton.txt')
Reply
#3
(May-17-2018, 05:55 AM)Larz60+ Wrote: place it in quotes:
load_atlas('ala-acton.txt')
Thank you for your help.When i place in the quotes the program is going to open a file called "filename" which does not exist

here is the error message
Error:
line 6, in load_atlas with open('filename', 'r') as csv_file: FileNotFoundError: [Errno 2] No such file or directory: 'filename'
may I ask do we have a method to modify it?

(May-17-2018, 05:54 AM)Tony Wrote: #3
I got it!!!again,Thank you for you help,I was confused by this for a while.
Reply
#4
you need to find the file.
Reply
#5
import csv
 
def load_atlas(filename):
    edited_file = []
    with open(filename, 'r') as csv_file:
        csv_reader = csv.reader(csv_file, delimiter='|')
        for lines in csv_reader:
            edited_file.append(lines)
    return edited_file
load_atlas('ala-acton.txt')
you need quotes on line 10 and don't need them on line 5 (see above). That is assuming the txt file is in the current working directory
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  mutable argument in function definition akbarza 1 426 Dec-15-2023, 02:00 PM
Last Post: deanhystad
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 516 Nov-23-2023, 02:53 PM
Last Post: rob101
  Printing the variable from defined function jws 7 1,166 Sep-03-2023, 03:22 PM
Last Post: deanhystad
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 1,033 Dec-25-2022, 03:00 PM
Last Post: askfriends
  Getting NameError for a function that is defined JonWayn 2 1,057 Dec-11-2022, 01:53 PM
Last Post: JonWayn
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 2,776 Nov-11-2022, 09:03 PM
Last Post: deanhystad
  i want to use type= as a function/method keyword argument Skaperen 9 1,776 Nov-06-2022, 04:28 AM
Last Post: Skaperen
  How to print the output of a defined function bshoushtarian 4 1,237 Sep-08-2022, 01:44 PM
Last Post: deanhystad
  User-defined function to reset variables? Mark17 3 1,592 May-25-2022, 07:22 PM
Last Post: Gribouillis
  Regex - Pass Flags as a function argument? muzikman 6 3,485 Sep-06-2021, 03:43 PM
Last Post: muzikman

Forum Jump:

User Panel Messages

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