remove double backslash from a list in python - python

i trying to add a path to list but removing the double backslash from each item in the list but i couldn't make this happened.
code :
import os
import shutil
sorc = r'D:\Try\Sorc'
dest = r'D:\Try\Dest'
#variables for sorc
folder_in_sorc = []
files_in_sorc = []
#variables for dest
folder_in_dest = []
files_in_dest = []
for root_sorc, dirs_sorc, files_sorc in os.walk(sorc):
for folder in dirs_sorc:
folder_in_sorc.append(os.path.join(os.path.abspath(root_sorc), folder))
for root_dest, dirs_dest, files_dest in os.walk(dest):
for folder in dirs_dest:
files_in_dest.append(os.path.join(root_dest, folder))
print(folder_in_sorc)

Try using forward slashes (I know you are using windows) , they work for me so try it once i guess. Good Luck :)
import os
import shutil
sorc = 'D:/Try/Sorc'
dest = 'D:/Try/Dest'
#variables for sorc
folder_in_sorc = []
files_in_sorc = []
#variables for dest
folder_in_dest = []
files_in_dest = []
for root_sorc, dirs_sorc, files_sorc in os.walk(sorc):
for folder in dirs_sorc:
folder_in_sorc.append(os.path.join(os.path.abspath(root_sorc), folder))
for root_dest, dirs_dest, files_dest in os.walk(dest):
for folder in dirs_dest:
files_in_dest.append(os.path.join(root_dest, folder))
print(folder_in_sorc)

You are printing a list, so Python prints out the internal representation of the list because it is not 1:1 convertible to a string.
Since backslashes are escape character in Python, they need to be escaped itself.
To solve your issue, print each element in your list to get the real string:
import os
import shutil
sorc = r'C:\temp'
dest = r'C:\temp2'
...
for folder in folder_in_sorc:
print(folder)
Out:
C:\temp\In
C:\temp\libui
C:\temp\pugi
C:\temp\py2exe-0.6.9
C:\temp\pylibui
C:\temp\pyNuklear
C:\temp\python-exe-unpacker
C:\temp\python-qt5
C:\temp\libui\.git

Related

separate filename after underlining _ os.path

In my path /volume1/xx/ are several files with this character A_test1.pdf, B_test2.pdf, ...I want to seperate the test1 part without path and .pdf.
Im newbie so I tried first with full name
but I got only the "*.pdf" as a text.
What is wrong with the path oder placeholder * ?
splitname = os.path.basename('/volume1/xx/*.pdf')
Edit
I got 2019-01-18_RG-Telekom[] from orign ReT_march - I want 2019-01-18_RG-Telekom_march (text after underlining) xx is a folder
here is the whole code:
#!/usr/bin/env python3
import datetime
import glob
import os
import os.path
SOURCE_PATH = '/volume1/xx'
TARGET_PATH = os.path.join(SOURCE_PATH, 'DMS')
def main():
today = datetime.date.today()
splitnames = [os.path.basename(fpath) for fpath in glob.glob("./xx/*.pdf")]
for prefix, name_part in [
('ReA', 'RG-Amazon'),
('GsA', 'GS-Amazon'),
('ReT', 'RG-Telekom'),
('NoE', 'Notiz-EDV'),
]:
filenames = glob.iglob(os.path.join(SOURCE_PATH, prefix + '*.pdf'))
for old_filename in filenames:
new_filename = os.path.join(TARGET_PATH, '{}_{}_{}.pdf'.format(today, name_part, splitnames))
os.rename(old_filename, new_filename)
if __name__ == '__main__':
main()
Use glob, os.path don't know how to process masks, but glob.glob works:
splitnames = [os.path.basename(fpath) for fpath in glob.glob("./**/*.txt")]
splitnames
Out:
['A_test1.pdf', 'B_test2.pdf']
Output of the glob:
glob.glob("./**/*.txt")
Out:
['./some_folder/A_test1.pdf', './another_folder/B_test2.pdf']
Apply os.path.basename to this list and extract basenames, as it shown above.
Edit
If xx in the path volume1/xx/ is just a folder name, not a mask, you should use following expression:
splitnames = [os.path.basename(fpath) for fpath in glob.glob("./xx/*.txt")]
because ./**/ is expression which masks a folder name and it's unnecessary that case.

Printing a value from a randint

import os
import random
path = os.listdir(r"file path here")
list = [os.listdir(r"life path here")]
print(len(path))
for i in range(len(path)):
full_path = (r"file path here" + path[i])
print(full_path)
print_random_items = random.randint(0, len(path[i]))
print(print_random_items)
So Hi I would like to know how I can print the name of the file associated with the value return to print(print_random_items)
ex: If the value is 15 I would like to print the 15th files name
First time asking a question here sorry if the format is wrong.
Don't bother with the random numbers. Just use random.choice:
random.choice(paths)
For example:
>>> import random
>>> paths = os.listdir('./exampledir')
>>> paths
['a.txt', 'b.txt', 'c.txt', 'd.txt', 'e.txt', 'f.txt']
>>> random.choice(paths)
'e.txt'
>>> random.choice(paths)
'c.txt'
Note: I see in your code that you are not familiar with python style iteration.
This
for i in range(len(path)):
full_path = (r"file path here" + path[i])
print(full_path)
is better written as
for partial_path in path:
full_path = r"file path here" + partial_path
print(full_path)
So rather than using range(len(path)) to get an index you can just iterate over path directly.

Renaming multiple images with .rename and .endswith

I've been trying to get this to work, but I feel like I'm missing something. There is a large collection of images in a folder that I need to rename just part of the filename. For example, I'm trying to rename the "RJ_200", "RJ_600", and "RJ_60"1 all to the same "RJ_500", while keeping the rest of the filename intact.
Image01.Food.RJ_200.jpg
Image02.Food.RJ_200.jpg
Image03.Basket.RJ_600.jpg
Image04.Basket.RJ_600.jpg
Image05.Cup.RJ_601.jpg
Image06.Cup.RJ_602.jpg
This is what I have so far, but it keeps just giving me the "else" instead of actually renaming any of them:
import os
import fnmatch
import sys
user_profile = os.environ['USERPROFILE']
dir = user_profile + "\Desktop" + "\Working"
print (os.listdir(dir))
for images in dir:
if images.endswith("RJ_***.jpg"):
os.rename("RJ_***.jpg", "RJ_500.jpg")
else:
print ("Arg!")
The Python string method endswith does not do pattern-matching with *, so you're looking for filenames which explicitly include the asterisk character and not finding any.
Try using regular expressions to match your filenames and then building your target filename explicitly:
import os
import re
patt = r'RJ_\d\d\d'
user_profile = os.environ['USERPROFILE']
path = os.path.join(user_profile, "Desktop", "Working")
image_files = os.listdir(path)
for filename in image_files:
flds = filename.split('.')
try:
frag = flds[2]
except IndexError:
continue
if re.match(patt, flds[2]):
from_name = os.path.join(path, filename)
to_name = '.'.join([flds[0], flds[1], 'RJ_500', 'jpg'])
os.rename(from_name, os.path.join(path, to_name))
Note that you need to do your matching with the file's basename and join on the rest of the path later.
You don't need to use .endswith. You can split the image file name up using .split and check the results. Since there are several suffix strings involved, I've put them all into a set for fast membership testing.
import os
import re
import sys
suffixes = {"RJ_200", "RJ_600", "RJ_601"}
new_suffix = "RJ_500"
user_profile = os.environ["USERPROFILE"]
dir = os.path.join(user_profile, "Desktop", "Working")
for image_name in os.listdir(dir):
pieces = image_name.split(".")
if pieces[2] in suffixes:
from_path = os.path.join(dir, image_name)
new_name = ".".join([pieces[0], pieces[1], new_suffix, pieces[3]])
to_path = os.path.join(dir, new_name)
print("renaming {} to {}".format(from_path, to_path))
os.rename(from_path, to_path)

Python - Loop through list within regex

Right, i'm relatively new to Python, which you will likely see in my code, but is there any way to iterate through a list within regex?
Basically, i'm looping through each filename within a folder, getting a code (2-6 digits) from the filename, and i'm wanting to compare it with a list of codes in a text file, which have a name attached, in the format "1234_Name" (without the quotation marks). If the code exists in both lists, I want to print out the list entry, i.e. 1234_Name. Currently my code only seems to look at the first entry in the text file's list and i'm not sure how to make it look through them all to find matches.
import os, re
sitesfile = open('C:/Users/me/My Documents/WORK_PYTHON/Renaming/testnames.txt', 'r')
filefolder = r'C:/Users/me/My Documents/WORK_PYTHON/Renaming/files/'
sites = sitesfile.read()
site_split = re.split('\n', sites)
old = []
newname = []
for site in site_split:
newname.append(site)
for root, dirs, filenames in os.walk(filefolder):
for filename in filenames:
fullpath = os.path.join(root, filename)
filename_split = os.path.splitext(fullpath)
filename_zero, fileext = filename_split
filename_zs = re.split("/", filename_zero)
filenm = re.search(r"[\w]+", str(filename_zs[-1:]))#get only filename, not path
filenmgrp = filenm.group()
pacode = re.search('\d\d+', filenmgrp)
if pacode:
pacodegrp = pacode.group()
match = re.match(pacodegrp, site)
if match:
print site
Hope this makes sense - thanks a lot in advance!
So, use this code instead:
import os
import re
def locate(pattern = r'\d+[_]', root=os.curdir):
for path, dirs, files in os.walk(os.path.abspath(root)):
for filename in re.findall(pattern, ' '.join(files)):
yield os.path.join(path, filename)
..this will only return files in a folder that match a given regex pattern.
with open('list_file.txt', 'r') as f:
lines = [x.split('_')[0] for x in f.readlines()]
print_out = []
for f in locate(<your code regex>, <your directory>):
if f in lines: print_out.append(f)
print(print_out)
...find the valid codes in your list_file first, then compare the files that come back with your given regex.

not listing entire directory

Im new on Python, Im actually on a short course this week, but I have a very specific request and I dont know how to deal with it right now: I have many different txt files in a folder, when I use the following code I receive only the filename of two of the many files, why is this?
regards!
import dircache
lista = dircache.listdir('C:\FDF')
i = 0
check = len(lista[0])
temp = []
count = len(lista)
while count != 0:
if len(lista[i]) != check:
temp.append(lista[i- 1])
check = len(lista[i])
else:
i = i + 1
count = count - 1
print (temp)
Maybe you can use the glob library: http://docs.python.org/2/library/glob.html
It seems that it works UNIX-like for listing files so maybe it can work with this?
import glob
directory = 'yourdirectory/'
filelist = glob.glob(directory+'*.txt')
If I've understood you correct, you would like to get all files?
Try it in this case:
import os
filesList = None
dir = 'C:\FDF'
for root, dirs, files in os.walk(dir):
filesList = files
break
print(filesList)
If need full path use:
import os.path
filesList = None
dir = 'C:\FDF'
for root, dirs, files in os.walk(dir):
for file in files:
filesList.append(os.path.join(root, file))
print(filesList)

Categories