trying to store a file in a string python - python

This is one of my first python projects, and i'm trying to make a script that would write a script which can re-create the src/ directory. this would be what i distribute to users. It uses walk, and writes a python file that first creates all the directories, and then writes the files. The issue i have is making the the files into a single string that i can write to a file.
This is the program i have:
import os
import pickle
src = os.path.dirname(os.path.realpath(__file__)) + os.sep + 'src'
fPack = 'import os \nimport pickle \nmyDir = os.path.dirname(os.path.realpath(__file__))'
Pack =''
print 'Packing ' + src
pickle
for root, dirs, files in os.walk(src, topdown=True):
for name in files:
print os.path.join(root, name)
f = open(os.path.join(root, name), 'r')
Pack = Pack + '\nf = open(os.path.join(myDir,\'' + name + '\'), \'w\')'
fileCont = pickle.dumps(f.read())
Pack = Pack + '\nf.write(pickle.loads(\'' + fileCont + '\'))'
for name in dirs:
print os.path.join(root, name)
fPack = fPack + '\nos.makedirs(os.path.join(myDir,\'' + name + '\'))'
print '==================================================\n\n\n'
print fPack + Pack
f = open(os.getcwd() + os.sep + 'dist' + os.sep + 'Pack.py', 'w')
f.write(fPack)
f.write(Pack)
And if i run it in a directory with on subdirectory, and on file inside it creates this file
import os
import pickle
myDir = os.path.dirname(os.path.realpath(__file__))
os.makedirs(os.path.join(myDir,'SphereText'))
f = open(os.path.join(myDir,'TextMain.py'), 'w')
f.write(pickle.loads('S"########################################################\n#Main SphereText file. #\n#SpereText is a simple Notepad-Like plain text editor #\n########################################################\n\nfrom Tkinter import *\nfrom tkFileDialog import *\nimport tkSimpleDialog\n\nroot = Tk()\nroot.title('SphereText')\n\ndef fSave():\n fileName = asksaveasfilename(parent=root)\n f = open(fileName, 'w')\n f.write(text.get(1.0,END))\n\ndef fOpen():\n fileName = ''\n fileName = askopenfilename(parent=root)\n f = open(fileName, 'r')\n text.delete(1.0,END)\n text.insert(1.0, f.read())\n\ndef tReplace():\n Old = tkSimpleDialog.askstring('SphereText', 'Replace:')\n print Old\n New = tkSimpleDialog.askstring('SphereText', 'With:')\n print New\n content = text.get(1.0,END)\n content = content.replace(Old, New)\n text.delete(1.0,END)\n text.insert(1.0, content)\n \nmenubar = Menu(root)\nmenubar.add_command(label='Save', command=fSave)\nmenubar.add_command(label='Open', command=fOpen)\nmenubar.add_command(label='Replace', command=tReplace)\nroot.config(menu=menubar)\n\ntext = Text(root, wrap=WORD)\n\ntext.pack()\n\nroot.mainloop()\n"
p0
.'))
The 's aren't escaped, and there are two line breaks at the end. i thought that the whole point of serializing was that you could always read it back the same way. Anyone know how i can mak the file a valid string?

Sorry about the newbish question, i just found out i had been trying to reinvent the wheel. apparently, that already exists under the name Squeeze.

Related

File has moved to other folder. But I can't rename it in spyder

import os
import shutil
src_folder = r"C:\new1\\"
dst_folder = r"C:\new2\\"
file_name = 'testword.docx'
if os.path.exists(dst_folder + file_name):
data = os.path.splitext(file_name)
only_name = data[0]
extension = data[1]
new_base = only_name + 'Renamed' + extension
new_name = os.path.join(dst_folder, new_base)
shutil.move(src_folder + file_name, new_name)
else:
shutil.move(src_folder + file_name, dst_folder + file_name)
I was trying to write a code to move a file from one folder to another and rename it. The file is moving to another folder, But I can't rename it. I am doing this using python(spyder). Can anyone help me with this.

NameError("name 'filename' is not defined",)

I have this error when I'm trying to run this code, maybe anyone can help me and give me directions.
After that problem will be solved, I need to create a loop that searching in all the xml files and finding a string, if that sting is in the file than it will move the file to the directory, anyone have an idea how can I do it?
import os
import glob
import shutil
def remove_ext(list_of_pathnames):
"""
removes the extension from each filename
"""
return [os.path.splitext(filename)[0] for filename in list_of_pathnames]
path = os.getcwd()
os.chdir("D:\\TomProject\\Images\\")
os.mkdir("Done\\") # create a new folder
newpath = os.path.join("D:\\TomProject\\","image_with_xml") # made it os independent...
list_of_jpgs = glob.glob(path+"*.jpg")
list_of_xmls = glob.glob(path+"*.xml")
list_of_txts = glob.glob(path+"*.txt")
print(list_of_jpgs, "\n\n", list_of_xmls, "\n\n", list_of_txts) #remove
jpgs_without_extension = remove_ext(list_of_jpgs)
xmls_without_extension = remove_ext(list_of_xmls)
txts_without_extension = remove_ext(list_of_txts)
for filename in jpgs_without_extension:
if filename in xmls_without_extension:
if filename in txts_without_extension:
print("moving", filename) #remove
shutil.move(filename + '*.jpg', newpath) # move image to new path.
shutil.move(filename + '*.xml', newpath)
shutil.move(filename + '*.txt', newpath)
make sure all jpg, xml, txt are in the current working directory. you may add one line to check current directory with
print(path)
please change the lines
list_of_jpgs = glob.glob(path+"*.jpg")
list_of_xmls = glob.glob(path+"*.xml")
list_of_txts = glob.glob(path+"*.txt")
to
list_of_jpgs = glob.glob(os.path.join(path,"*.jpg"))
list_of_xmls = glob.glob(os.path.join(path,"*.xml"))
list_of_txts = glob.glob(os.path.join(path,"*.txt"))
last three lines should be indented. and * should be removed.
for filename in jpgs_without_extension:
if filename in xmls_without_extension:
if filename in txts_without_extension:
print("moving", filename) #remove
shutil.move(filename + '.jpg', newpath) # move image to new path.
shutil.move(filename + '.xml', newpath)
shutil.move(filename + '.txt', newpath)

Python: Change file names to the names of people in a list

I have a couple slides, each slide corresponds to a person. I need to name each file (.pptx) after the individual name it references. A lot of the examples I see on mass renaming have the renaming become sequential like:
file1
file2
file3
I need:
bob.pptx
sue.pptx
jack.pptx
I was able to change names using os found on this site https://www.marsja.se/rename-files-in-python-a-guide-with-examples-using-os-rename/:
import os, fnmatch
file_path = 'C:\\Users\\Documents\\Files_To_Rename\\Many_Files\\'
files_to_rename = fnmatch.filter(os.listdir(file_path), '*.pptx')
print(files_to_rename)
new_name = 'Datafile'
for i, file_name in enumerate(files_to_rename):
new_file_name = new_name + str(i) + '.pptx'
os.rename(file_path + file_name,
file_path + new_file_name)
But again, this just names it:
Datafile1
Datafile2
etc
my example
import os from pathlib
import Path
files = os.listdir("c:\\tmp\\")
for key in range(0, len(files)):
print (files[key])
os.rename("c:\\tmp\\" + files[key], "c:\\tmp\\" + files[key].replace("-",""))
Path("c:\\tmp\\" + files[key] + '.ok').touch() # if u need add some extension
Here's how I ran your code (avoiding file paths I don't have!), getting it to print output not just rename
import os, fnmatch
file_path = '.\\'
files_to_rename = fnmatch.filter(os.listdir(file_path), '*.pptx')
print(files_to_rename)
new_name = 'Datafile'
for i, file_name in enumerate(files_to_rename):
new_file_name = new_name + str(i) + '.pptx'
print (file_path + new_file_name)
os.rename(file_path + file_name,
file_path + new_file_name)
This gave me
.\Datafile0.pptx
.\Datafile1.pptx
...
and did give me the correct sequence of pptx files in that folder.
So I suspect the problem is that you are getting the file names you want, but you can't see them in Windows. Solution: show file types in Windows. Here's one of many available links as to how: https://www.thewindowsclub.com/show-file-extensions-in-windows
Thank you everyone for your suggestions, I think I found it with a friend's help:
import os, fnmatch
import pandas as pd
file_path = 'C:\\Users\\Documents\\FolderwithFiles\\'
files_to_rename = fnmatch.filter(os.listdir(file_path), '*.pptx') #looks for any .ppt in path, can make any ext
df = pd.read_excel('Names.xlsx') #make a list of names in an xl, this won't read the header, header should be Names, then list your names)
for i, file_name in zip(df['Names'], files_to_rename): #zip instead of a nest for loop
new_file_name = i + '.pptx'
os.rename(file_path + file_name, file_path + new_file_name)
print(new_file_name)

What is a proper way to write data to a file?

Im trying to write some data to a file but I have some problems with the path Im using.
This is my code:
my_path = r'c:\data\XYM\Desktop\MyFolder 7-sep'
with open(my_path + '\\' + 'Vehicles_MM' + '\\' + name_vehicile + '-AB.txt', 'w') as output:
writer = csv.writer(output, delimiter = '\t')
writer.writerow(headers)
writer.writerow(data)
for vehicle_loc_list in vehicle_loc_dict.values():
for record_group in group_records(vehicle_loc_list):
writer.writerow(output_record(record_group))
This is the error I receive:
FileNotFoundError: [Errno 2] No such file or directory: 'c:\\data\\XYM\\Desktop\\MyFolder 7-sep\\Vehicles_MM\\20200907-AB.txt'
Based on revelations in comments, the problem is that you are trying to write to a subdirectory c:\data\XYM\Desktop\MyFolder 7-sep\Vehicle_MM\ which doesn't exist, and which actually you don't want to write into.
The fix is to remove the directory separator \\; maybe use a different separator instead. For example,
with open(my_path + '\\' + 'Vehicles_MM-' + name_vehicile + '-AB.txt', 'w') as output:
If you did want to write to this subdirectory, you have to make sure it exists before you attempt to open a file inside it.
os.makedirs(my_path + '\\' + 'Vehicles_MM', exist_ok=True)
with open(...
The same thing is somewhat more readable with pathlib.Path;
from pathlib import Path
my_path = Path(r'c:\data\XYM\Desktop\MyFolder 7-sep')
vehicles_mm = my_path / 'Vehicles_MM'
vehicles_mm.mkdir(parents=True, exist_ok=True)
filename = vehicles_m / name_vehicile + '-AB.txt'
with filename.open('w') as output:
...
You should use one of the builtins to work with paths. Either os.path or pathlib.Path
# with os.path:
import os.path as p
filename = p.join(my_path, "Vehicles_MM", name_vehicle + "-AB.txt")
assert p.exists(p.dirname(filename))
# with pathlib.Path:
from pathlib import Path
my_path = Path("c:\data\XYM\Desktop\MyFolder 7-sep")
filename = my_path.joinpath("Vehicles_MM", name_vehicle + "-AB.txt")
assert filename.parent.exists()

How do I get every file of an extension from a directory to another? I wrote a code but I'm getting an exception

This piece of code is my first attempt at creating a program. I'm getting an error when running it that reads:
PermissionError: [WinError 32] The process cannot access the file
because it is being used by another process:
'C:\Users\gabri\Desktop\' -> 'C:\Users\gabri\Desktop\Planilhas
Excel\'
What am I doing wrong? The goal of this program is to get all excel, then pdf, then word files and put them in folders created by the program.
import os
from glob import glob
# import cx_Freeze
print("Digite o diretório de origem.")
dirOrigem = input()
os.chdir(dirOrigem)
excel_files = glob('*.xlsx')
excel_files.append(''.join(glob('*.xls')))
dirDestinoXL = dirOrigem + '\\' + 'Planilhas Excel'
if not os.path.exists(dirDestinoXL):
os.makedirs(dirDestinoXL)
for i in excel_files:
os.rename(f'{dirOrigem}\\{"".join(i)}', f'{dirDestinoXL}\\{"".join(i)}')
os.chdir(dirOrigem)
pdf_files = glob('*.pdf')
dirDestinoPDF = dirOrigem + '\\' + 'PDF'
if not os.path.exists(dirDestinoPDF):
os.makedirs(dirDestinoPDF)
for p in pdf_files:
os.rename(f'{dirOrigem}\\{"".join(p)}', f'{dirDestinoPDF}\\{"".join(p)}')
os.chdir(dirOrigem)
word_files = glob('*.doc')
word_files.append(glob('*.docx'))
dirDestinoWord = dirOrigem + '\\' + 'Word'
if not os.path.exists(dirDestinoWord):
os.makedirs(dirDestinoWord)
for d in word_files:
os.rename(f'{dirOrigem}\\{"".join(d)}', f'{dirDestinoWord}\\{"".join(d)}')
I tried your program and it doesn't work as it is on my computer. I changed some lines and it works. Hope it helps
import os
from glob import glob
dirOrigem = r'C:\Users\fchal\Desktop\temp' # here I changed the code just because I didn't want to bother using input()
os.chdir(dirOrigem)
excel_files = glob('*.xlsx')
excel_files.extend(glob('*.xls'))
dirDestinoXL = dirOrigem + '\\' + 'xlsfile'
if not os.path.exists(dirDestinoXL):
os.makedirs(dirDestinoXL)
for i in excel_files:
os.rename(i, os.path.join(dirDestinoXL, i))
# same procedure for pdf and word files
I know that glob can be a mess sometimes. And if the files are open, you can get errors. Here's what I would do:
import os
def move_files_with_extension(from_dir, to_dir, *extensions):
if not os.path.isdir(from_dir):
raise ValueError('{} is not a real directory'.format(from_dir))
elif not os.path.isdir(to_dir):
raise ValueError('{} is not a real directory'.format(to_dir))
files_with_extensions = all_files_with_extensions_in(from_dir, *extensions)
for file_path in files_with_extensions:
os.rename(file_path, os.path.join(to_dir, os.path.basename(file_path)))
def all_files_with_extensions_in(dir, *extensions):
files_with_extensions = list()
for dir_path, dir_names, file_names in os.walk(dir):
for file_name in file_names:
if file_name.endswith(extensions):
files_with_extensions.append(os.path.join(dir_path, file_name))
return files_with_extensions
and then you can do:
dirOrigem = input()
excel_location = os.path.join(dirOrigem, 'Planilhas Excel')
move_files_with_extension(dirOrigem, excel_location, '.xls', '.xlsx')
and so on

Categories

Resources