Python Forum
XARGS - help passing results
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
XARGS - help passing results
#1
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.
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Search Results Web results Printing the number of days in a given month and year afefDXCTN 1 2,233 Aug-21-2020, 12:20 PM
Last Post: DeaD_EyE
  How to append one function1 results to function2 results SriRajesh 5 3,145 Jan-02-2020, 12:11 PM
Last Post: Killertjuh

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020