Search code examples
pythonstringintegerindices

Getting error while replacing placeholders in Python


I recently used an anonymous file sharing site anonymousfiles.io. It has an api. So, I looked into it and found

import requests
r =  requests.post('https://api.anonymousfiles.io', files={'file': open('#1.txt', 'rb')})
print(r.text)

this code. I tried to make a small console app with this peice of code.

print()
print('ANONYMOUSFILES.IO - File Uploader(API)')
print()
print()

import requests

try:
    while True:
        print('Enter the file path for upload')
        file_path = input(':-')

        if file_path == '' or file_path == ' ':
            print()
            print()
            print('Please enter a valid file path')
        else:
            fu =  requests.post('https://api.anonymousfiles.io', files={'file': open(file_path, 'rb')})
            upload=fu.text
            print('''File Name: {0}
                     File Size: {1}
                     File Type: {2}

                     **********

                     {4}
                  '''.format(upload['name'],upload['size'],upload['mime_type'],upload['url']))

except FileNotFoundError:
    print('Please enter a valid file path')

This is my code. Now the problem is whenever I execute it, it shows an error saying

Traceback (most recent call last):
  File "I:\Python Exports\Fumk.py", line 27, in <module>
    '''.format(upload['name'],upload['size'],upload['mime_type'],upload['url']))
TypeError: string indices must be integers

upload cointains

>>> upload
'{"url": "https://anonymousfiles.io/3ASBCuxh/", "id": "3ASBCuxh", "name": "Fumk.py", "size": 840}'

So, how do I replace the placeholders without getting an error?


Solution

  • The response returned by the API will be a string in JSON format, so you will need to convert this into a python dictionary before you can format your printed string.

    That is why it says TypeError: string indices must be integers because the response is a string and you are trying to index it with another string. (e.g. "Hello"["World"])

    We can use the loads method from the json package in the python standard library to achieve this.

    Secondly, the API no longer returns the mime-type of the file, so I have removed this.

    Try this:

    print()
    print('ANONYMOUSFILES.IO - File Uploader(API)')
    print()
    print()
    
    
    ## for loading a JSON string into a dictionary
    from json import loads
    
    import requests
    
    try:
        while True:
            print('Enter the file path for upload')
            file_path = input(':-')
    
            if file_path == '' or file_path == ' ':
                print()
                print()
                print('Please enter a valid file path')
            else:
                fu =  requests.post('https://api.anonymousfiles.io', files={'file': open(file_path, 'rb')})
                # Convert to a dictionary before parsing
                upload = loads(fu.text)
                print('''
                         File Name: {0}
                         File Size: {1}
    
                         **********
    
                         {2}
                      '''.format(upload['name'],upload['size'],upload['url']))
    
    except FileNotFoundError:
        print('Please enter a valid file path')