2

I'm trying to get info about how much GPU % is currently used. I'm running script below and get result.

import os


get_total_gpu = 'C:\Windows\System32\DriverStore\FileRepository\\nvmdi.inf_amd64_36ae6ddd01b54f49\\nvidia-smi ' \
               '--query-gpu=memory.total--format=csv'


val=os.system(get_total_gpu )

print(val)
memory.total [MiB]
4096 MiB
1

Process finished with exit code 0

However, the result is in weird format and I cannot get the value from it (4096 MiB. I have GTX 1650). I ran type(val) and it gives me class int. How can this be an integer? This more looks like a formatted string. Please explain me what's happening here and how can I get the number.

Thanks in advance!

3
  • Does this answer your question? read from subprocess output python Commented Nov 20, 2023 at 15:12
  • 2
    the return value of system is the exit code of the process, not it's captured standard or error output Commented Nov 20, 2023 at 15:13
  • The first two lines of your output are from running the command in your get_total_gpu string. The 1 in the third line is from your print(val) - please read the docs on the things you are using, don't make weird assumption. As @Homer512 pointed out, you should use a different method to get the return value of your command string and then you'll have to parse it. Commented Nov 20, 2023 at 15:14

1 Answer 1

1

its in csv format, so

import os
import csv
import subprocess

# Command to get total GPU memory using nvidia-smi
get_total_gpu = 'C:\\Windows\\System32\\DriverStore\\FileRepository\\nvmdi.inf_amd64_36ae6ddd01b54f49\\nvidia-smi --query-gpu=memory.total --format=csv'

# Run the command and capture the output
output = subprocess.check_output(get_total_gpu, shell=True, text=True)

# Split the output into lines
lines = output.strip().split('\n')

# Use csv.reader to parse the CSV data
csv_reader = csv.reader(lines)

# Skip the header row if it exists
next(csv_reader)

# Read the second row
second_row = next(csv_reader)

# Get the value from the first column
value = second_row[0]

# Convert the value to an integer (assuming it's a number)
value_as_int = int(value)

# Do something with value_as_int
print(value_as_int)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.