0

I am working with deep learning project in JUPYTER NOTEBOOK

# Loading a text file into memory
def load_doc(filename):
    # Opening the file as read only
    file = open("C:\\Users\Project\Flickr8k.token.txt", "r")
    text = file.read()
    file.close()
    return text

By using function I need to load a File and display a File Contents but not getting any output.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 2
    Are you calling your function and printing the output anywhere? Please provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). Note that `load_doc` doesn't even use the `filename` parameter at the moment. – dspencer Apr 09 '20 at 10:16
  • Iam not able to understand this functions concept def load_doc(filename) – Srikanth Bhattu Apr 09 '20 at 10:23
  • I have a linked to the official tutorial in my answer: https://docs.python.org/3/tutorial/controlflow.html#defining-functions – dspencer Apr 09 '20 at 10:25

3 Answers3

2

Right now, it looks like you are not calling your load_doc function anywhere, hence the lack of output. You also don't use the filename parameter in your function. I recommend you seek out a tutorial to learn about how to use functions in python - see the official tutorial.

As an aside, you should look to use the context manager approach to opening a file, since this ensures that the file handle is closed, even if an exception occurs while the file is open.

def load_doc(filename):
    # Opening the file as read only
    with open(filename, 'r') as fh:
        return fh.read()

# Call the function with my file path
content = load_doc("C:\\Users\Project\Flickr8k.token.txt")
# Print the output
print(content)
dspencer
  • 4,297
  • 4
  • 22
  • 43
  • ok but iam Getting This Error---- IOPub data rate exceeded. The notebook server will temporarily stop sending output to the client in order to avoid crashing it. To change this limit, set the config variable `--NotebookApp.iopub_data_rate_limit`. Current values: NotebookApp.iopub_data_rate_limit=1000000.0 (bytes/sec) NotebookApp.rate_limit_window=3.0 (secs) – Srikanth Bhattu Apr 09 '20 at 10:28
  • Make sure you're using the latest version of `jupyter`. You may also increase the `iopub_data_rate_limit` yourself using `jupyter notebook --NotebookApp.iopub_data_rate_limit=1.0e10`. (https://stackoverflow.com/questions/43288550/iopub-data-rate-exceeded-in-jupyter-notebook-when-viewing-image) – dspencer Apr 09 '20 at 10:30
1

you should call load_doc(), Also as you have you path in function no need for filename .check this

def load_doc():
    # Opening the file as read only
    file = open("C:\\Users\Project\Flickr8k.token.txt, "r")
    text = file.read()
    file.close()
    return text

print(load_doc())
Mohsen
  • 1,079
  • 2
  • 8
  • 23
0

It's easier here to use "with open()" as it handles all the tricky stuff with the files automatically. It also needs to be a raw string to escape the \ otherwise python gets confused thinking you're putting in a control character with open(r"C:\Users\lewis\example\example.txt", 'r') as file: data = f2.read() print(data) after the indentation the file is automatically closed

lewis
  • 19
  • 3