Python Forum

Full Version: XARGS - help passing results
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
System = RHEL 7
Python v = 2.7

Using the code below, I am using file_list and random_choice to randomly select one filename from a given DIR of many files

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import random
import subprocess
file_list = ['/MyDir/bin/messages/messageone.txt',
'/MyDir/bin/messages/messagetwo.txt',
'/MyDir/bin/messages/messagethree.txt',
'/MyDir/bin/messages/messagefour.txt',
'/MyDir/bin/messages/messagefive.txt',
'/MyDir/bin/messages/messagesix.txt',
'/MyDir/bin/messages/messageseven.txt',
'/MyDir/bin/messages/messageeight.txt',
'/MyDir/bin/messages/messagenine.txt',
'/MyDir/bin/messages/messageten.txt' ]
strOut = random.choice(file_list)
print strOut
The random file selected output currently looks like this:
/MyDir/bin/messages/messagesix.txt

Using XARGS (or some other means) I would like to take that output and construct and execute a simple CP command, which effectively copies the randomly selected file to another directory with filename message.txt, something like the below.

cp strOut /MyDir/bin/message.txt

Normally, I would post my attempted code, but my attempts have been feeble and miserable failures at best.

Any help would be greatly appreciated.
if you want to invoke system command like cp take a look at subprocess module
in the simplest form
import subprocess
subprocess.run(['cp', source, destination]) # replace source and destination with your own or assign values beforehand
also you can use pure python to copy files