Copy directory contents into a directory with python [duplicate] - python

This question already has answers here:
How do I copy an entire directory of files into an existing directory using Python?
(15 answers)
Closed 4 years ago.
I have a directory /a/b/c that has files and subdirectories.
I need to copy the /a/b/c/* in the /x/y/z directory. What python methods can I use?
I tried shutil.copytree("a/b/c", "/x/y/z"), but python tries to create /x/y/z and raises an error "Directory exists".

I found this code working which is part of the standard library:
from distutils.dir_util import copy_tree
# copy subdirectory example
from_directory = "/a/b/c"
to_directory = "/x/y/z"
copy_tree(from_directory, to_directory)
Reference:
Python 2: https://docs.python.org/2/distutils/apiref.html#distutils.dir_util.copy_tree
Python 3: https://docs.python.org/3/distutils/apiref.html#distutils.dir_util.copy_tree

You can also use glob2 to recursively collect all paths (using ** subfolders wildcard) and then use shutil.copyfile, saving the paths
glob2 link: https://code.activestate.com/pypm/glob2/

from subprocess import call
def cp_dir(source, target):
call(['cp', '-a', source, target]) # Linux
cp_dir('/a/b/c/', '/x/y/z/')
It works for me. Basically, it executes shell command cp.

The python libs are obsolete with this function. I've done one that works correctly:
import os
import shutil
def copydirectorykut(src, dst):
os.chdir(dst)
list=os.listdir(src)
nom= src+'.txt'
fitx= open(nom, 'w')
for item in list:
fitx.write("%s\n" % item)
fitx.close()
f = open(nom,'r')
for line in f.readlines():
if "." in line:
shutil.copy(src+'/'+line[:-1],dst+'/'+line[:-1])
else:
if not os.path.exists(dst+'/'+line[:-1]):
os.makedirs(dst+'/'+line[:-1])
copydirectorykut(src+'/'+line[:-1],dst+'/'+line[:-1])
copydirectorykut(src+'/'+line[:-1],dst+'/'+line[:-1])
f.close()
os.remove(nom)
os.chdir('..')

Related

List the content of a folder using python [duplicate]

This question already has answers here:
How can I list the contents of a directory in Python?
(8 answers)
Closed 4 years ago.
Is it possible to output the contents of a folder in python ?
If, say you had a function and you would pass the folder path to its argument?
What would be the simplest way for a beginner to take?
By "simplest" I mean, without using any modules.
Note
Google usually leads me to stackoverflow. Plus I am not looking for generic answers. I am looking for insight based on experience and wit, which I can only find here.
You could use os.listdir() to look at the contents of a directory. It takes the path as an argument, for example:
import os
directory = os.listdir('/')
for filename in directory:
print(filename)
prints this for me:
bin
home
usr
sbin
proc
tmp
dev
media
srv
etc
lib64
mnt
boot
run
var
sys
lib
opt
root
.dockerenv
run_dir
How about listdir? Let's try like this way-
import os
def ShowDirectory:
return os.listdir("folder/path/goes/here")
ShowDirectory()
With function.
def ListFolder(folder):
import os
files = os.listdir(folder)
print(*files,sep="\n")
Please use google first next time...it seems you are looking for this:
How can I list the contents of a directory in Python?
The accepted answer there:
import os
os.listdir("path") # returns list

Script delete C:\Windows\CSC\v2.0.6\namespace [duplicate]

This question already has answers here:
How to delete the contents of a folder?
(27 answers)
Closed 9 years ago.
I am trying to create a Python script to Delete everying under C:\Windows\CSC\v2.0.6\namespace
I need an Idea.. to do it in the command line i have to go to cmd an then psexec -s cmd than i have to goto C:\Windows\CSC\v2.0.6\namespace and than rd *what ever folder there.. i want to create a script to remove all.. any help
This code should delete any files or directories in your directory:
import os, shutil
folder = "C:\Windows\CSC\v2.0.6\namespace"
for item in os.listdir(folder):
path = os.path.join(folder, item)
try:
os.unlink(path) # delete if the item is a file
except Exception as e:
shutil.rmtree(path) # delete if the item is a folder
This has been answered previously.
A simple Google search and a few modifications:
import os
mainPath = "C:\Windows\CSC\v2.0.6\namespace"
files = os.listdir(mainPath)
for f in files:
os.remove('{}/{}'.format(mainPath, f))
If you want to recursively find all of the files and THEN delete them all (this is a small script I wrote yesterday):
import os, os.path
def getAllFiles(mainPath):
subPaths = os.listdir(mainPath)
for path in subPaths:
pathDir = '{}\{}'.format(mainPath, path)
if os.path.isdir(pathDir):
paths.extend(getAllFiles(pathDir, paths))
else:
paths.append(pathDir)
return paths
So then you can do:
files = getAllFiles(mainPath)
for f in files:
os.remove(f)
Note: the recursive algorithm gets somewhat slow (and may raise a MemoryError) if there are too many subfolders (it creates a lot of recursive nodes).
To avoid this, you can use the recursive function as a helper function, which is called by a main iterative function:
def getDirs(path):
sub = os.listdir(path)
paths = []
for p in sub:
pDir = '{}\{}'.format(path, p)
if os.path.isdir(pDir):
paths.extend(getAllFiles(pDir, paths)) # getAllFiles is the same as above
else:
paths.append(pDir)
return paths
It get's slow for very large subfolders, however. Going through C:\Python27\Lib takes about 6-7 seconds for me (it has about 5k+ files in it, and many, many subfolders).

Find the current directory and file's directory [duplicate]

This question already has answers here:
How do you properly determine the current script directory?
(16 answers)
How to know/change current directory in Python shell?
(7 answers)
Closed 5 years ago.
How do I determine:
the current directory (where I was in the shell when I ran the Python script), and
where the Python file I am executing is?
To get the full path to the directory a Python file is contained in, write this in that file:
import os
dir_path = os.path.dirname(os.path.realpath(__file__))
(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)
To get the current working directory use
import os
cwd = os.getcwd()
Documentation references for the modules, constants and functions used above:
The os and os.path modules.
The __file__ constant
os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
os.path.dirname(path) (returns "the directory name of pathname path")
os.getcwd() (returns "a string representing the current working directory")
os.chdir(path) ("change the current working directory to path")
Current working directory: os.getcwd()
And the __file__ attribute can help you find out where the file you are executing is located. This Stack Overflow post explains everything: How do I get the path of the current executed file in Python?
You may find this useful as a reference:
import os
print("Path at terminal when executing this file")
print(os.getcwd() + "\n")
print("This file path, relative to os.getcwd()")
print(__file__ + "\n")
print("This file full path (following symlinks)")
full_path = os.path.realpath(__file__)
print(full_path + "\n")
print("This file directory and name")
path, filename = os.path.split(full_path)
print(path + ' --> ' + filename + "\n")
print("This file directory only")
print(os.path.dirname(full_path))
The pathlib module, introduced in Python 3.4 (PEP 428 — The pathlib module — object-oriented filesystem paths), makes the path-related experience much much better.
pwd
/home/skovorodkin/stack
tree
.
└── scripts
├── 1.py
└── 2.py
In order to get the current working directory, use Path.cwd():
from pathlib import Path
print(Path.cwd()) # /home/skovorodkin/stack
To get an absolute path to your script file, use the Path.resolve() method:
print(Path(__file__).resolve()) # /home/skovorodkin/stack/scripts/1.py
And to get the path of a directory where your script is located, access .parent (it is recommended to call .resolve() before .parent):
print(Path(__file__).resolve().parent) # /home/skovorodkin/stack/scripts
Remember that __file__ is not reliable in some situations: How do I get the path of the current executed file in Python?.
Please note, that Path.cwd(), Path.resolve() and other Path methods return path objects (PosixPath in my case), not strings. In Python 3.4 and 3.5 that caused some pain, because open built-in function could only work with string or bytes objects, and did not support Path objects, so you had to convert Path objects to strings or use the Path.open() method, but the latter option required you to change old code:
File scripts/2.py
from pathlib import Path
p = Path(__file__).resolve()
with p.open() as f: pass
with open(str(p)) as f: pass
with open(p) as f: pass
print('OK')
Output
python3.5 scripts/2.py
Traceback (most recent call last):
File "scripts/2.py", line 11, in <module>
with open(p) as f:
TypeError: invalid file: PosixPath('/home/skovorodkin/stack/scripts/2.py')
As you can see, open(p) does not work with Python 3.5.
PEP 519 — Adding a file system path protocol, implemented in Python 3.6, adds support of PathLike objects to the open function, so now you can pass Path objects to the open function directly:
python3.6 scripts/2.py
OK
To get the current directory full path
>>import os
>>print os.getcwd()
Output: "C :\Users\admin\myfolder"
To get the current directory folder name alone
>>import os
>>str1=os.getcwd()
>>str2=str1.split('\\')
>>n=len(str2)
>>print str2[n-1]
Output: "myfolder"
Pathlib can be used this way to get the directory containing the current script:
import pathlib
filepath = pathlib.Path(__file__).resolve().parent
If you are trying to find the current directory of the file you are currently in:
OS agnostic way:
dirname, filename = os.path.split(os.path.abspath(__file__))
If you're using Python 3.4, there is the brand new higher-level pathlib module which allows you to conveniently call pathlib.Path.cwd() to get a Path object representing your current working directory, along with many other new features.
More info on this new API can be found here.
To get the current directory full path:
os.path.realpath('.')
Answer to #1:
If you want the current directory, do this:
import os
os.getcwd()
If you want just any folder name and you have the path to that folder, do this:
def get_folder_name(folder):
'''
Returns the folder name, given a full folder path
'''
return folder.split(os.sep)[-1]
Answer to #2:
import os
print os.path.abspath(__file__)
I think the most succinct way to find just the name of your current execution context would be:
current_folder_path, current_folder_name = os.path.split(os.getcwd())
If you're searching for the location of the currently executed script, you can use sys.argv[0] to get the full path.
For question 1, use os.getcwd() # Get working directory and os.chdir(r'D:\Steam\steamapps\common') # Set working directory
I recommend using sys.argv[0] for question 2 because sys.argv is immutable and therefore always returns the current file (module object path) and not affected by os.chdir(). Also you can do like this:
import os
this_py_file = os.path.realpath(__file__)
# vvv Below comes your code vvv #
But that snippet and sys.argv[0] will not work or will work weird when compiled by PyInstaller, because magic properties are not set in __main__ level and sys.argv[0] is the way your executable was called (it means that it becomes affected by the working directory).

Better way to zip files in Python (zip a whole directory with a single command)? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I zip the contents of a folder using python (version 2.5)?
Suppose I have a directory: /home/user/files/. This dir has a bunch of files:
/home/user/files/
-- test.py
-- config.py
I want to zip this directory using ZipFile in python. Do I need to loop through the directory and add these files recursively, or is it possible do pass the directory name and the ZipFile class automatically adds everything beneath it?
In the end, I would like to have:
/home/user/files.zip (and inside my zip, I dont need to have a /files folder inside the zip:)
-- test.py
-- config.py
Note that this doesn't include empty directories. If those are required there are workarounds available on the web; probably best to get the ZipInfo record for empty directories in our favorite archiving programs to see what's in them.
Hardcoding file/path to get rid of specifics of my code...
target_dir = '/tmp/zip_me_up'
zip = zipfile.ZipFile('/tmp/example.zip', 'w', zipfile.ZIP_DEFLATED)
rootlen = len(target_dir) + 1
for base, dirs, files in os.walk(target_dir):
for file in files:
fn = os.path.join(base, file)
zip.write(fn, fn[rootlen:])
You could try using the distutils package:
distutils.archive_util.make_zipfile(base_name, base_dir[, verbose=0, dry_run=0])
You might also be able to get away with using the zip command available in the Unix shell with a call to os.system
You could use subprocess module:
import subprocess
PIPE = subprocess.PIPE
pd = subprocess.Popen(['/usr/bin/zip', '-r', 'files', 'files'],
stdout=PIPE, stderr=PIPE)
stdout, stderr = pd.communicate()
The code is not tested and pretends to works just on unix machines, i don't know if windows has similar command line utilities.

What is the Python equivalent of Perl's FindBin? [duplicate]

This question already has answers here:
How do you properly determine the current script directory?
(16 answers)
Closed 5 years ago.
In Perl, the FindBin module is used to locate the directory of the original script. What's the canonical way to get this directory in Python?
Some of the options I've seen:
os.path.dirname(os.path.realpath(sys.argv[0]))
os.path.abspath(os.path.dirname(sys.argv[0]))
os.path.abspath(os.path.dirname(__file__))
You can try this:
import os
bindir = os.path.abspath(os.path.dirname(__file__))
That will give you the absolute path of the current file's directory.
I don't use Python very often so I do not know if there is package like FindBin but
import os
import sys
bindir = os.path.abspath(os.path.dirname(sys.argv[0]))
should work.
To update on the previous answers, with Python 3.4+, you can now do:
import pathlib
bindir = pathlib.Path(__file__).resolve().parent
Which will give you the same, except you'll get a Path object that is way more nice to work with.

Categories