Home > Software engineering >  How to move newly exported files to new folders each time the are created?
How to move newly exported files to new folders each time the are created?

Time:12-14

I'm working with the internet_stability library in python and I want to use it to, as the name implies test my internet stability and also speed.

The code I use works perfectly, but instead of using it only once, I would like to loop the code so that it can run throughout the day.

The problem I have is that library uses it's own exporting functions and that whenever you rerun the program it overwrites the old files.

So, I think a solution for this would be to create new folder every time it loops and put the exported files of that loop into the folders.

How would I go about doing this? The code I use is shown below.

import network_stability

net = network_stability.NetworkTest()

# Run connectivity test.

net.connection_test_interval(hours=0.1)
net.export_connection_results('connection.csv')
net.report_connection('connection.png')

# Run speed test.

net.speed_test_interval(minutes=10)
net.export_speed_results('speed.csv')
net.report_speed('speed.png')

CodePudding user response:

import network_stability
import datetime

net = network_stability.NetworkTest()

# Run connectivity test.

net.connection_test_interval(hours=0.1)
net.export_connection_results(f'{datetime.datetime.now().strftime("%y_%m_%d-%H_%M_%S")}_connection.csv')
net.report_connection(f'{datetime.datetime.now().strftime("%y_%m_%d-%H_%M_%S")}_connection.png')

# Run speed test.

net.speed_test_interval(minutes=10)
net.export_speed_results(f'{datetime.datetime.now().strftime("%y_%m_%d-%H_%M_%S")}_speed.csv')
net.report_speed(f'{datetime.datetime.now().strftime("%y_%m_%d-%H_%M_%S")}_speed.png')

It will create files like this:

>>> f'{datetime.datetime.now().strftime("%y_%m_%d-%H_%M_%S")}_speed.png'                                                
'21_12_13-11_31_22_speed.png'

Because filenames are differents, it won t overwrite them. (Unless your run twice tests a second!)

If you want all you files has the same name in a run, do this:

import network_stability
import datetime

net = network_stability.NetworkTest()

date = datetime.datetime.now().strftime("%y_%m_%d-%H_%M_%S")

# Run connectivity test.

net.connection_test_interval(hours=0.1)
net.export_connection_results(f'{date}_connection.csv')
net.report_connection(f'{date}_connection.png')

# Run speed test.

net.speed_test_interval(minutes=10)
net.export_speed_results(f'{date}_speed.csv')
net.report_speed(f'{date}_speed.png')

CodePudding user response:

For similar situations I use a simple workaround:

run_number = 0
save_name = "Connection"   str(run_number) 

net.export_speed_results(save_name   '.csv')
net.report_speed(save_name   '.png')

run_number  = 1

Maybe that works for your problem as well?

  • Related