Python Forum

Full Version: Any idea?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
New to Python but not to coding.

I would like to copy only the hidden files and folders in my home directory with rsync from a Python script.
The issues is that using a .* 101 games after the source directory name (/home/jer/.*) causes an rsync error when executed from Python.
However, in the Linux terminal, this statement works fine:

rsync -an /home/jer/.*/ /Temp

After some research, I tried the following syntax:

rsync -an /home/jer/.[^.]*/ /Temp
This also causes an rsync error but in the Linux terminal also works fine.

Below is my relevant code snippet:

bk_args = "-avn"
bk_src_dir = "/home/jer/.[^.]*/"
bk_tar_dir = "/mnt/md0"
bk_options = ['--itemize-changes=%i%f', '--info=stats2', '--progress']

try:
    subprocess.run(["rsync", bk_args, *bk_options, bk_src_dir, bk_tar_dir])
This causes the following rsync error:
sending incremental file list
rsync: [sender] change_dir "/home/jer/.[^.]*" failed: No such file or directory (2)
I have tried various combinations of escaping ".*" without success so I thought it was time to come to the mountain for some ideas!
I use rsync regularly, but never from Python. Why would you do that?

Look here in linuxquestions.org, an answer from michaelk, who has helped me many times!

Quote:arg="rsync -avn "+ bk_src_dir + " " + bk_tar_dir
subprocess.run(arg,shell=True)

Basically, he made a string called arg:

Quote:"rsync -avn "+ bk_src_dir + " " + bk_tar_dir

then handed it to subprocess.run()

Either this post in lq was you, or you are copying it!

Canondale said this works in a terminal:

Quote:rsync -a /home/jer/.[^.]*/ /Temp