Sam Dover
Sam Dover

Reputation: 1

Python - Calculate file transfer time

I am fumbling my way through learning python and need some help getting started on some things.

For this project I need to create a program that calculates file transfer time. I need to prompt the user for a file size in megabytes.

I also need to prompt the user for the estimated transfer speed in megabits per second. If the transfer time is more than one minute I need to display the time in minutes.

Where should I start? I know I will need to have some user input (obviously), import sys, and use sys.argv somewhere...

Upvotes: 0

Views: 3305

Answers (2)

Adam Kerz
Adam Kerz

Reputation: 951

Prompting the user:

raw_input('Prompt String: ')

Timing:

import datetime
start=datetime.datetime.now()
# do stuff
end=datetime.datetime.now()

Calculation of time:

(end-start).seconds/60 # end-start gives a datetime.timedelta object

I don't quite follow why you're asking the user for a file size instead of just reading the file size from the file system (os.path.getsize).

Upvotes: 0

pboennig
pboennig

Reputation: 13

First, you need to prompt the user for their file size and transfer speed, obviously.

I usually use input. The reason is that sys takes arguments directly after the name of your file in the command line, so you'd say python file.py 10 11. I find creating a better user interface for taking input via prompts to be more logical. so for your case:

file_size = input("File Size in MB: ") speed = input("Transfer speed in Megabits/second: ")

Now taking those parameters, you have to take into account that one megabyte = 8 megabits, and then do simple unit conversions to come to the final answer of seconds. You asked for a start, so I'll refrain from simply posting a solution.

NOTE: This uses Python 3. The Python 2 version would use raw_input.

Upvotes: 1

Related Questions