How can I get the size of a single file with Python?
Asked by Dongying_Cao:

Hello! I want to **get the size of a specific file** in my repository. There's a solution in the documentation: <https://developer.atlassian.com/server/bitbucket/rest/v811/api-group-repository/#api-api-latest-projects-projectkey-repos-repositoryslug-browse-path-get>

However, it says that "If true only the size will be returned for the file path instead of the contents". I don't understand what does the size for the file path mean.

Besides, when I followed the example python script below to request the size, the result was 158 instead of 9.6kb ( the actual size). What does 158 mean?

Thanks in advance.

```
# This code sample uses the 'requests' library: 

# http://docs.python-requests.org 

import requests 

import json url = "http://{baseurl}/rest/api/latest/projects/{projectKey}/repos/{repositorySlug}/browse/{path}" 

headers = { "Accept": "application/json" } 

response = requests.request( "GET", url, headers=headers )

 print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))

```


Answers:
author: Dave Chevell
body: Hey [@Dongying_Cao](/t5/user/viewprofilepage/user-id/5244657) ,

Short answer {#toc-hId--463295396}
----------------------------------

**158** is the number of lines in the file. append **?size=true** to your request, and you'll get a response like **{'size': 9600}**

```
r = requests.get(f'{baseurl}/rest/api/latest/projects/{projectKey}/repos/{repositorySlug}/browse/{path}?size=true')

print(r.json())
```

Long answer {#toc-hId-2024217437}
---------------------------------

The source of confusion here is that "size" has two different meanings.

1. File size: this is what you're trying to get, and this *only appears when you set **size=true** on your request*
2. Resource size: a generic property of all rest API's, that describes the number of results being returned in a given API request

When no other options are set, the standard **/{path}** resource returns the *contents* of the file. Each line is returned as a separate item inside a **lines** array. As with all our API's, the returned JSON has a number of standard values as well: **limit** (max number of results to be returned in a request), **isLastPage** (if results exceed the limit and are spread over multiple pages, this tells you whether you've reached the last page), and **size** , which in *this* context just means, the number of results being returned in this request, which is the number of lines in the file, or in the **lines** array.

You asked:
> However, it says that "If true only the size will be returned for the file path instead of the contents". I don't understand what does the size for the file path mean.

When you *do* set **size=true**, that's when you're actually querying the file size. You won't get the file contents back, you'll get the file size value instead.

Even though the word "size" is used both times, it's being used to mean different things, and our API documentation is *only*referring to the explicit use of this option to get file size instead of the file contents.

I hope that clears it up!

Cheers,

Dave


Tags: bitbucket-server

python

repository

rest-api

server