How can you check the contents of a file with python, and then copy a file from the same folder and move it to a new location?
I have Python 3.1 but i can just as easily port to 2.6
thank you!
for example
import os,shutil
root="/home"
destination="/tmp"
directory = os.path.join(root,"mydir")
os.chdir(directory)
for file in os.listdir("."):
flag=""
#check contents of file ?
for line in open(file):
if "something" in line:
flag="found"
if flag=="found":
try:
# or use os.rename() on local
shutil.move(file,destination)
except Exception,e: print e
else:
print "success"
If you look at the shutil doc, under .move() it says
shutil.move(src, dst)ΒΆ
Recursively move a file or directory to another location.
If the destination is on the current filesystem, then simply use rename.
Otherwise, copy src (with copy2()) to the dst and then remove src.
I guess you can use copy2() to move to another file system.
os.listdir() and shutil.move().
Related
I would like to save a CSV file in a specific folder, but I can't find anywhere how to do it...
this is the code
# Writing on a CSV FILE
fileToWrite = open(f"{userfinder}-{month}-{year}.csv', "a")
fileToWrite.write('Subject,Start Date,Start Time,End Date,End Time,All Day Event,Description\n')
fileToWrite.write(f'{string1}{tagesinfo2},{soup_datum},{soup_dienstbegin},{soup_datum},{soup_dienstende},,Kommentar: {soup_kommentar} Schiff: {b} Funktion: {soup_funktion} Schichtdauer: {soup_schichtdauer} Bezahlte Zeit: {soup_bezahltezeit} Mannschaft: {crew_list2}\n')
fileToWrite.close()
print(f'Datum: {soup_datum} Dienst: {string1}{tagesinfo2} --> Mannschaft: {crew_list2} --> OK')
You just have to change the working directory with os.chdir(path):
import os
path = '/Users/user/folder/example sub folder'
os.chdir(path)
#your code here
or, as mentioned in the comments, you can use:
myfolder = "c:/foo/bar/"
fileToWrite = open(f"{myfolder}/{userfinder}-{month}-{year}.csv", "a")
#in this case the path is "{myfolder}/{userfinder}-{month}-{year}"
This option includes the path when opening (only affects the one file) whereas os.chdir() changes the directory for everything (what I use personally for all of my projects, which are small).
If you don't want to change your folder for all files created and read, use the second option; but when you want a python file to affect every file in a distant location I would use os.chdir().
I have tried to use shutil, but rather than deleting the contents of the folder it just deletes the whole folder.
def delete_song():
print("Deleting song")
shutil.rmtree('./song_downloads')
print("Deleted song")
However it didn't print out "Deleted song". I also tried to use os.remove()
def delete_song():
print("Deleting song")
for file in os.listdir('./song_downloads'):
os.remove(file)
print("Deleted file")
But this didn't seem to work. Thanks
As mentioned in the comment from #grysik on your question, the output of os.listdir() only gives unqualified file names (e.g. no path), therefore the call to os.remove() won't be able to find the file in the current working directory, so you need to pass the path in as well.
The below will do what you require:
def delete_song(directory):
print("Deleting song...")
for f in os.listdir(directory):
qualified_file=os.path.join(directory, f)
os.remove(qualified_file)
print(f"Deleted file [{qualified_file}]")
try using pathlib which allows you to work with the entire Path of the file so you can remove it, and print out the qualified file name.
from pathlib import Path
def delete_song(dir : str) -> None:
for file in Path(dir).glob('*'):
print(f'Removing song --> {file.stem}')
file.unlink()
print('Songs deleted.')
If you only want to remove files in a directory, you can try this -
from pathlib import Path
[f.unlink() for f in Path("/path/to/folder").glob("*") if f.is_file()]
I'm trying to create a shell script that will copy files from one computer (employee's old computer) to another (employee's new computer). I have it to the point where I can copy files over, thanks to the lovely people here, but I'm running into a problem - if I'm going from, say, this directory that has 2 files:
C:\Users\specificuser\Documents\Test Folder
....to this directory...
C:\Users\specificuser\Desktop
...I see the files show up on the Desktop, but the folder those files were in (Test Folder) isn't created.
Here is the copy function I'm using:
#copy function
def dir_copy(srcpath, dstpath):
#if the destination path doesn't exist, create it
if not os.path.exists(dstpath):
os.makedir(dstpath)
#tag each file to the source path to create the file path
for file in os.listdir(srcpath):
srcfile = os.path.join(srcpath, file)
dstfile = os.path.join(dstpath, file)
#if the source file path is a directory, copy the directory
if os.path.isdir(srcfile):
dir_copy(srcfile, dstfile)
else: #if the source file path is just a file, copy the file
shutil.copyfile(srcfile, dstfile)
I know I need to create the directory on the destination, I'm just not quite sure how to do it.
Edit: I found that I had a type (os.makedir instead of os.mkdir). I tested it, and it creates directories like it's supposed to. HOWEVER I'd like it to create the directory one level up from where it's starting. For example, in Test Folder there is Sub Test Folder. It has created Sub Test Folder but won't create Test Folder because Test Folder is not part of the dstpath. Does that make sense?
You might want to look at shutil.copytree(). It performs the recursive copy functionality, including directories, that you're looking for. So, for a basic recursive copy, you could just run:
shutil.copytree(srcpath, dstpath)
However, to accomplish your goal of copying the source directory to the destination directory, creating the source directory inside of the destination directory in the process, you could use something like this:
import os
import shutil
def dir_copy(srcpath, dstdir):
dirname = os.path.basename(srcpath)
dstpath = os.path.join(dstdir, dirname)
shutil.copytree(srcpath, dstpath)
Note that your srcpath must not contain a slash at the end for this to work. Also, the result of joining the destination directory and the source directory name must not already exist, or copytree will fail.
This is a common problem with file copy... do you intend to just copy the contents of the folder or do you want the folder itself copied. Copy utilities typically have a flag for this and you can too. I use os.makedirs so that any intermediate directories are created also.
#copy function
def dir_copy(srcpath, dstpath, include_directory=False):
if include_directory:
dstpath = os.path.join(dstpath, os.path.basename(srcpath))
os.makedirs(dstpath, exist_ok=True)
#tag each file to the source path to create the file path
for file in os.listdir(srcpath):
srcfile = os.path.join(srcpath, file)
dstfile = os.path.join(dstpath, file)
#if the source file path is a directory, copy the directory
if os.path.isdir(srcfile):
dir_copy(srcfile, dstfile)
else: #if the source file path is just a file, copy the file
shutil.copyfile(srcfile, dstfile)
import shutil
import os
def dir_copy(srcpath, dstpath):
try:
shutil.copytree(srcpath, dstpath)
except shutil.Error as e:
print('Directory not copied. Error: %s' % e)
except OSError as e:
print('Directory not copied. Error: %s' % e)
dir_copy('/home/sergey/test1', '/home/sergey/test2')
I use this script to backup (copy) my working folder. It will skip large files, keep folder structure (hierarchy) and create destination folders if they don't exist.
import os
import shutil
for root, dirs, files in os.walk(the_folder_copy_from):
for name in files:
if os.path.getsize(os.path.join(root, name))<10*1024*1024:
target=os.path.join("backup", os.path.relpath(os.path.join(root, name),start=the_folder_copy_from))
print(target)
os.makedirs(os.path.dirname(target),exist_ok=True)
shutil.copy(src=os.path.join(root, name),dst=target)
print("Done")
How can I tell python to scan the current directory for a file called "filenames.txt" and if that file isn't there, to extract it from a zip file called "files.zip"? I know how to work zipfile, I just don't know how to scan the current directory for that file and use if/then loops with it..
import os.path
try:
os.path.isFile(fname)
# play with the file
except:
# unzip file
import os, zipfile
if 'filenames.txt' in os.listdir('.'):
print 'file is in current dir'
else:
zf = zipfile.ZipFile('files.zip')
zf.extract('filenames.txt')
From the documentation
$ pydoc os.path.exists
Help on function exists in os.path:
os.path.exists = exists(path)
Test whether a path exists. Returns False for broken symbolic links
I am writing a script to backup files from one dir(Master) to another dir(Clone).
And the script will monitor the two directories.
If a file inside clone is missing then the script will copy the missing file from Master to
Clone.Now I have a problem creating the missing folder.
I have read the documentation and found that shutil.copyfile will create a dir if the
dir doesn't exist.But I am getting an IOError message showing that the destination dir
is not exist.Below is the code.
import os,shutil,hashlib
master="C:\Users\Will Yan\Desktop\Master"
client="D:\Clone"
if(os.path.exists(client)):
print "PATH EXISTS"
else:
print "PATH Doesn't exists copying"
shutil.copytree(master,client)
def walkLocation(location,option):
aList = []
for(path,dirs,files) in os.walk(location):
for i in files:
if option == "path":
aList.append(path+"/"+i)
else:
aList.append(i)
return aList
def getPaths(location):
paths=[]
files=[]
result =[]
paths = walkLocation(location,'path')
files = walkLocation(location,'files')
result.append(paths)
result.append(files)
return result
ma=walkLocation(master,"path")
cl=walkLocation(client,"path")
maf=walkLocation(master,"a")
clf=walkLocation(client,"a")
for i in range(len(ma)):
count = 0
for j in range(len(cl)):
if maf[i]==clf[j]:
break
else:
count= count+1
if count==len(cl):
dirStep1=ma[i][ma[i].find("Master")::]
dirStep2=dirStep1.replace("Master",client)
shutil.copyfile(ma[i],dirStep2)
Can anyone tell me where did I do wrong?
Thanks
Sorry, but the documentation doesn't say that. Here's a reproduction of the full documentation for the function:
shutil.copyfile(src, dst)
Copy the
contents (no metadata) of the file
named src to a file named dst. dst
must be the complete target file name;
look at copy() for a copy that accepts
a target directory path. If src and
dst are the same files, Error is
raised. The destination location must
be writable; otherwise, an IOError
exception will be raised. If dst
already exists, it will be replaced.
Special files such as character or
block devices and pipes cannot be
copied with this function. src and dst
are path names given as strings.
So you have to create the directory yourself.