Python Forum
XARGS - help passing results - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: XARGS - help passing results (/thread-20281.html)



XARGS - help passing results - 3inchpython - Aug-03-2019

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.


RE: XARGS - help passing results - buran - Aug-03-2019

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