import os
import sys
import pathlib
for folderName,subfolders,filenames in os.walk('/'):
for filename in filenames:
# print(filename)
if filename.endswith('.pdf'):
path=pathlib.Path(filename).parent.absolute()
print("the file "+str(filename)+" has path "+str(path))
I want this script to look for all the pdf files in the os and i also want to print the path of the file but when i run the script it print the file names but prints the path in which i have the python script and not print the path to the pdf file.
This should work:
import os
import sys
import pathlib
for folderName,subfolders,filenames in os.walk('/'):
for filename in filenames:
if filename.endswith('.pdf'):
print(f"the file {filename} has path {folderName}")
You don't need pathlib for this one.
pathlib.Path(filename) will consider filename as a relative path, and thus its parent will be the folder from which the script was runned.
Related
this doesn’t delete the folder
shutil.rmtree('C:\\Users\\0\\Downloads\\preoutput')
this one causes this error
Message=[WinError 5] Access is denied: 'C:\Users\0\Downloads\testdelete'
import os
import glob
files = glob.glob('C:\\Users\\0\\Downloads\\preoutput')
for f in files:
#os.chmod(f, 0o777)
os.remove(f)
Try to implement this program using shutil.rmtree()
import shutil
import os
#location
location = "C:\\Users\\0\\Downloads\\preoutput"
#directory
dir = "preoutput"
#path
path = os.path.join(location, dir)
#removing directory
shutil.rmtree(path)
I want to know if and how it's possible to revert a move operation or deconstruct the file that was created. I wrote a script that should've moved all nested jpg files into a folder but instead wrote it to a file. This is due to an error in the target path, I missed the slash at the end to denote it was folder.
This is the code that created the mistake:
#!/usr/bin/python3
from sys import argv
from os import walk, path
from fnmatch import fnmatch
from shutil import move
rootdir = argv[1]
for root, dirs, files in walk(rootdir):
for filename in files:
if fnmatch(filename, '*.jpg'):
print(f'moving {filename} to {rootdir}')
origin = path.join(root, filename)
target = path.join(rootdir, 'Pictures')
move(origin, target)
print('done.')
I am working on a program that will edit all local files ending in a csv extension. When I call the location of the directory and then change directory I get an error. The error is due to extra \'s being added to the path. How can I call the path without these extra \'s?
I've looked around and there are similar issues but every example I see is for a hard written location and not a movable one.
import os
import glob
import sys
path = os.path.abspath(__file__)
extension = '.csv'
os.chdir(os.path.abspath(__file__))
result = glob.glob('*'.format(extension))
print(path)
print(result)
os.chdir() needs a directory not a file which is what you are giving it. try changing os.chdir(os.path.abspath(__file__)) to os.chdir(os.path.dirname(path))
import os
import glob
import sys
__file__ = 'test.txt'
path = os.path.abspath(__file__)
print(path)
extension = '.csv'
os.chdir(os.path.dirname(path))
result = glob.glob('*'.format(extension))
print(path)
print(result)
I try make some scripts which helps me zip a file from selected dir.
I have:
import sys
import os
import zipfile
source_dir = "C:\\myDir\\yourDir\\"
zip = zipfile.ZipFile("C:\\myDir\\yourDirZip.zip","w",allowZip64=True)
for root, dirs, files in os.walk(source_dir):
for f in files:
zip.write(os.path.join(root,f))
zip.close()
After execution, in yourDirZip.zip is:
myDir/
yourDir/
...
I expect directly only yourDir or even only content of yourDir
Have you any ideas on how I can get what I want?
You can specify arcname parameter in the write method:
import sys
import os
import zipfile
source_dir = "C:\\myDir\\yourDir"
zip = zipfile.ZipFile("C:\\myDir\\yourDirZip.zip","w",allowZip64=True)
for root, dirs, files in os.walk(source_dir):
for f in files:
zip.write(os.path.join(root,f), arcname=f)
zip.close()
How do I obtain the short path of a file in Windows using python ?
I am using the following code ,
#!/usr/bin/python
import os
import sys
fileList = []
rootdir = sys.argv[1]
for root, subFolders, files in os.walk(rootdir):
for file in files:
fileList.append(os.path.join(root,file))
for File in fileList:
print File
I guess you are looking for this:
http://docs.activestate.com/activepython/2.5/pywin32/win32api__GetShortPathName_meth.html
Although you will need the win32api module for this.
Also see this link:
http://mail.python.org/pipermail/python-win32/2006-May/004697.html
import win32api
long_file_name='C:\Program Files\I am a file'
short_file_name=win32api.GetShortPathName(long_file_name)