rename or remove question mark from file name in python - python

I am actually trying to remove or rename files name which contains question mark in python.
Does anyone would have some guidance or experience to share?
my files are like this:
??test.txt
?test21.txt
test??1.txt
the result that I am trying to get is:
test.txt
test21.txt
test1.txt
thank you for your help.
AL
Below the code which I have attempted using the suggestion below:
#!/usr/bin/python
import sys, os, glob
for iFiles in glob.glob('*.txt'):
print (iFiles)
os.rename(iFiles, iFiles.replace("?",''))

This should do what you require.
import os
import sys
import argparse
parser = argparse.ArgumentParser(description='Rename files by replacing all instances of a character from filename')
parser.add_argument('--dir', help='Target DIR containing files to rename', required=True)
parser.add_argument('--value', help='Value to search for an replace', required=True)
args = vars(parser.parse_args())
def rename(target_dir, rep_value):
try:
for root, dirs, files in os.walk(target_dir):
for filename in files:
if rep_value in filename:
filename_new = str(filename).replace(rep_value, '')
os.rename(os.path.join(root, filename), os.path.join(root, filename_new))
print '{} renamed to {}'.format(filename, os.path.join(root, filename_new))
except Exception,e:
print e
target_dir = args['dir']
rep_value = args['value']
rename(target_dir, rep_value)
Example usage:
rename.py --dir /root/Python/ --value ?
Output
?test.txt renamed to /root/Python/test.txt
?test21.txt renamed to /root/Python/test21.txt
test1?.txt renamed to /root/Python/test1.txt

Related

How to copy files selected from a txt file to another folder python

Folder A has more than 100 files, folder B is my destination folder. I want to copy 10 files in folder A to folder B. The 10 files names are in the text file C.
import os
import shutil
from glob import glob
namelist = open('/Users/C.txt').read().splitlines()
input = '/Users/A'
output = '/Users/B'
path = '/Users/A'
files = glob(path)
for path in files:
filedir, filename = os.path.split(path)
for filename in namelist:
shutil.copy2(input,output)
It returns an Error. Please help me to do it in Python, thanks a lot!
There are a lot of things that you can do with your code:
import os
import shutil
from glob import glob
#namelist = open('/Users/C.txt').read().splitlines()
# context manager will take care of closing the file after open
# no need read as one string and do splitlines, readlines take care of that
with open('/Users/C.txt') as fp:
namelist = fp.readlines()
input = '/Users/A'
output = '/Users/B'
path = '/Users/A'
files = os.listdir(path)
# dont need glob import as you already imported os
#files = glob(path)
# loop only through files mentioned in the text file and see if they are available in
# folder A
for file_name in namelist:
file_path = os.path.join(input,file_name)
if file_path in files:
dest_path = os.path.join(output,file_name)
shutil.copy(file_path,dest_path)
#for path in files:
# filedir, filename = os.path.split(path)
# for filename in namelist:
# shutil.copy2(input,output)
I do not have sample data or error message to check. From what i can see in your code,
for path in files:
filedir, filename = os.path.split(path)
if filename in namelist:
shutil.copy2(input,output)
Your paths are from the root folder because of the starting forward slash. Try putting a dot in front of them if the folders and files are relative to the location of your .py file or no preceding slash:
./Users/A or Users/A

command line arguments using python

I have this code that will let the user choose which file he wants to update by passing an argument in the command line, and then it do some more things but I have not included that here:
import sys
import os
from sys import argv
path = "/home/Desktop/python/test"
files = os.walk( path )
filename = argv[1]
if filename in files:
inputFile = open(filename, 'r')
else:
print "no match found"
sys.exit()
inputFile.close()
When I run the script it keeps giving me "no match found" but im pretty sure the file is there. I cant see what Im doing wrong
os.walk() returns a generator, one that produces tuples with (root, directories, files) values for each iteration.
You can't use that generator to test for a single file, not with a simple in membership test.
You'll also need to re-instate the whole path; you can't just open an unclassified filename without the directory it lives in. Just use a for loop here, and break once you found it. The else suite on a for loop only executes when you did not use break (e.g. the file was not found):
path = "/home/Desktop/python/test"
filename = argv[1]
for root, directories, files in os.walk(path):
if filename in files:
full_path = os.path.join(root, filename)
break
else:
print "no match found"
sys.exit()
with open(full_path) as input_file:
# do something with the file
I added a with statement to handle the lifetime of the file object; once the with block is exited the file is automatically closed for you.
Alternatively, you may use following code snippet.
import os.path
filename = argv[1]
path = "/home/Desktop/python/test/"
if os.path.isfile(path + filename):
inputFile = open(path + filename, "r")
else:
print "File Not Found"

Rename the files' names

I'd like to change the files whose extension are '.test.txt' into '.txt'.
As my codes as below, it cannot work cause invalid syntax happened to the place of 'if'.
Could you please figure out it?
Thank you so much.
import sys
import os
path = "Dir"
for(dirpath,dirnames,files)in os.walk(path):
for filename in files:
filepath = os.path.join(dirpath,filename)
if '.test.txt' in filename:
newfilename = filename.replace('.test.txt','.txt')
os.rename(filename,newfilename)
this should work...
import sys
import os
path = r"Dir"
for dirpath,dirnames,files in os.walk(path):
for filename in files:
filepath = os.path.join(dirpath,filename)
if '.test.txt' in filename:
newfilename = filename.replace('.test.txt','.txt')
newfilepath = os.path.join(dirpath, newfilename)
os.rename(filepath, newfilepath)
you did not define the new file path, in renaming action you have to supply the full file path, os.rename(src_path, dest_path)

Python tarfile - check if file in tar exists outside (i.e., already been extracted)

I'm new to stackoverflow. Sorry if this post is redundant but I haven't found the answer yet. Also, I'm fairly new to Python. I'd like to extract files from a tar file if they do not already exist in the root directory where the tar file exists. I've tried a number of versions. I think there is some redundancy in the code below, and it doesn't do what I need it to. It just keeps extracting and overwriting the existing file(s).
Files that need to be extracted will always end in "_B7.TIF". Code currently takes one argument - the full path of the directory that contains the tar file.
import os, shutil, sys, tarfile
directory = sys.argv[1]
tifFiles = []
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith(".TIF"):
# also tried tifFiles.append(file)
tifFiles.append(file.name)
elif file.endswith(".tar.gz"):
tar = tarfile.open(root + "/" + file)
for item in tar:
if str(item) in tifFiles:
print "{0} has already been unzipped.".format(str(item))
elif "_B7" in str(item):
tar.extract(item, path=root)
shutil.rmtree(root + "\gap_mask")
Here is another version that does not appear to be doing anything. I was trying to simplify...
import os, shutil, sys, tarfile
directory = sys.argv[1]
for root, dirs, files in os.walk(directory):
if file not in tarfile.getnames() and file.endswith("_B7.TIF"):
tar.extract(file, path=root)
else:
print "File: {0} has already been unzipped.".format(file)
shutil.rmtree(root + "\gap_mask")
Thank you both for your comments/suggestions. They both helped in some way. This code works for me.
import os, shutil, sys, tarfile
folder = sys.argv[1]
listFiles = os.listdir(folder)
try:
for file in listFiles:
if file.endswith(".tar.gz"):
sceneTIF = file[:-7] + "_B7.TIF"
if os.path.exists(os.path.join(folder,sceneTIF)):
print sceneTIF, "has already been extracted."
else:
tar = tarfile.open(os.path.join(folder,file))
for item in tar:
if "_B7" in str(item):
tar.extract(item, path=folder)
shutil.rmtree(os.path.join(folder,"gap_mask")
except WindowsError:
pass
Any thoughts on style/redundancy/ways to make it better? Thomas, your code was not working straight out of the box. I think it was the tarfile.open component. Probably needed tarfile.open(os.path.join(directory, archive)). I only thought of that after reworking the above though. Haven't tested. Thanks again.
os.walk iterates over directory trees, including sub-directories. From your description that is not what you want. Also, only files that are encountered earlier than your tarfiles will be considered for existence.
It is a lot easier to just check for the existence of files you encounter:
import sys
import os
import tarfile
directory = sys.argv[1]
def extract_nonexisting(archive):
for name in archive.getnames():
if os.path.exists(os.path.join(directory, name)):
print name, "already exists"
else:
archive.extract(name, path=directory)
archives = [name for name in os.listdir(directory) if name.endswith("tar.gz")]
for archive_name in archives:
with tarfile.open(archive_name) as archive:
extract_nonexisting(archive)

ZIP folder with subfolder in python

I need to zip a folder that containts an .xml file and a .fgdb file by using python. Could anyone help me? I tried a few scripts I found on internet but there is always some technical issue (such as creating an empty zip file, or create zip file I cannot open 'no permission' etc..)
Thanks in advance.
The key to making it work is the os.walk() function. Here is a script I assembled in the past that should work. Let me know if you get any exceptions.
import zipfile
import os
import sys
def zipfolder(foldername, target_dir):
zipobj = zipfile.ZipFile(foldername + '.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)
zipobj.write(fn, fn[rootlen:])
zipfolder('thenameofthezipfile', 'thedirectorytobezipped') #insert your variables here
sys.exit()
This answer is very helpful, but it took me a moment to fully understand what rootlen was for. This example uses some longer variable names to help teach what exactly is going on here. Also included is a block to validate the zip file is correct.
import os
import zipfile
src_path = os.path.join('/', 'some', 'src', 'path')
archive_name = 'myZipFile.zip'
archive_path = os.path.join('/', 'some', 'path', archive_name)
with zipfile.ZipFile(archive_path, 'w', zipfile.ZIP_DEFLATED) as archive_file:
for dirpath, dirnames, filenames in os.walk(src_path):
for filename in filenames:
file_path = os.path.join(dirpath, filename)
archive_file_path = os.path.relpath(file_path, src_path)
archive_file.write(file_path, archive_file_path)
with zipfile.ZipFile(archive_path, 'r') as archive_file:
bad_file = zipfile.ZipFile.testzip(archive_file)
if bad_file:
raise zipfile.BadZipFile(
'CRC check failed for {} with file {}'.format(archive_path, bad_file))
If someone wants some generic function to utilize in your project
def zip_compression_tree(root_path, zip_name):
with zipfile.ZipFile(zip_name, 'w') as z:
for root, dirs, files in os.walk(root):
for file in files:
z.write(os.path.join(root, file))
for directory in dirs:
z.write(os.path.join(root, directory))

Categories

Resources