Getting short path in python - python

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)

Related

pathlib prints the current directory path

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.

How to move from one directory to another and delete only '.html' files in python?

I attended an interview and they asked me to write a script to move from one directory to another and delete only the .html files.
Now I tried to do this at first using os.remove() . Following is the code:
def rm_files():
import os
from os import path
folder='J:\\Test\\'
for files in os.listdir(folder):
file_path=path.join(folder,files)
os.remove(file_path)
The problem I am facing here is that I cannot figure out how to delete only .html files in my directory
Then I tried using glob. Following is the code:
def rm_files1():
import os
import glob
files=glob.glob('J:\\Test\\*.html')
for f in files:
os.remove(f)
Using glob I can delete the .html files but still I cannot figure out how to implement the logic of moving from one directory to another.
And along with that can someone please help me figure out how to delete a specific file type using os.remove() ?
Thank you.
Either of these methods should work. For the first way, you could just string.endswith(suffix) like so:
def rm_files():
import os
from os import path
folder='J:\\Test\\'
for files in os.listdir(folder):
file_path=path.join(folder,files)
if file_path.endswith(".html"):
os.remove(file_path)
Or if you prefer glob, moving directories is fairly straightforward: os.chdir(path) like this:
def rm_files1():
import os
os.chdir('J:\\Test')
import glob
files=glob.glob('J:\\Test\\*.html')
for f in files:
os.remove(f)
Though it seems unnecessary since glob is taking an absolute path anyway.
Your problem can be described in the following steps.
move to specific directory. This can be done using os.chdir()
grab list of all *.html files. Use glob.glob('*.html')
remove the files. use os.remove()
Putting it all together:
import os
import glob
import sys
def remove_html_files(path_name):
# move to desired path, if it exists
if os.path.exists(path_name):
os.chdir(path_name)
else:
print('invalid path')
sys.exit(1)
# grab list of all html files in current directory
file_list = glob.glob('*.html')
#delete files
for f in file_list:
os.remove(f)
#output messaage
print('deleted '+ str(len(file_list))+' files in folder' + path_name)
# call the function
remove_html_files(path_name)
To remove all html files in a directory with os.remove() you can do like this using endswith() function
import sys
import os
from os import listdir
directory = "J:\\Test\\"
test = os.listdir( directory )
for item in test:
if item.endswith(".html"):
os.remove( os.path.join( directory, item ) )

Why doesn't python standalone file manipulation function work?

I have a file test.py which works good. see code:
import os
import shutil
import re
for root, dirs, files in os.walk("../config/"):
for file in files:
print os.path.join(root, file)
if file.endswith(".txt") and file.startswith("default_"):
file_name = os.path.basename(os.path.join(root, file))
file_name = re.sub(r'default_','',file_name)
shutil.copy(os.path.join(root, file),os.path.join(root,file_name))
but when I wrapped the code into a function and put it in another file config.py. And I called the function in another file as config.copy_default_files(), it doesn't work. So I put a raw_input() in the end of the function to see if the function is executed, and it did print 'miao', but it didn't print out the list of files. And no file is generated or copied.
I am so so confused.Can someone explain it to me please? Any help would be great appreciated. Let me know if you need more information on it. Manythanks!
import os
import shutil
import re
def copy_default_files(work_dir = "../config/"):
for root, dirs, files in os.walk(work_dir):
for file in files:
print os.path.join(root, file)
if file.endswith(".txt") and file.startswith("default_"):
file_name = os.path.basename(os.path.join(root, file))
file_name = re.sub(r'default_','',file_name)
shutil.copy(os.path.join(root, file),os.path.join(root,file_name))
raw_input('miao')
return 0
Defining the function is not enough. You also need to call it:
copy_default_files()
or
config.copy_default_files()
(depending on whether you're running config.py as a script or importing it as a module).

Search files in folder using part of the name and save/copy to different folder using Python

I have 700 files in a single folder. I need to find files that have "h10v03" as part of the name and copy them to a different folder using python.
Heres an example of one of the files: MOD10A1.A2000121.h10v03.005.2007172062725.hdf
I appreciate any help.
Something like this would do the trick.
import os
import shutil
source_dir = "/some/directory/path"
target_dir = "/some/other/directory/path"
part = "h10v03"
files = [file for file in os.listdir(source_dir)
if os.path.isfile(file) and part in file]
for file in files:
shutil.copy2(os.path.join(source_dir, file), target_dir)
Does it need to be python?
A unix shell does that for you quite fine:
cp ./*h10v03* /other/directory/
In python I would suggest you take a look at os.listdir() and shutil.copy()
EDIT:
some untested code:
import os
import shutil
src_dir = "/some/path/"
target_dir = "/some/other/path/"
searchstring = "h10v03"
for f in os.listdir(src_dir):
if searchstring in f and os.path.isfile(os.path.join(src_dir, f)):
shutil.copy2(os.path.join(src_dir, f), target_dir)
print "COPY", f
with the glob module (untested):
import glob
import os
import shutil
for f in glob.glob("/some/path/*2000*h10v03*"):
print f
shutil.copy2(f, os.path.join("/some/target/dir/", os.path.basename(f)))
Firstly, find all the items in that folder with os.listdir. Then you can use the count() method of string to determine if it has your string. Then you can use shutil to copy the file.

Delete files with python through OS shell

Im Tyring to Delete all Files in E:.
with wildcard.
E:\test\*.txt
I would ask rather than test the os.walk.
In windows.
The way you would do this is use the glob module:
import glob
import os
for fl in glob.glob("E:\\test\\*.txt"):
#Do what you want with the file
os.remove(fl)
A slightly verbose writing of another method
import os
dir = "E:\\test"
files = os.listdir(dir)
for file in files:
if file.endswith(".txt"):
os.remove(os.path.join(dir,file))
Or
import os
[os.remove(os.path.join("E:\\test",f)) for f in os.listdir("E:\\test") if f.endswith(".txt")]
You could use popen for this as well if you want to do it in fewer lines
from subprocess import Popen
proc = Popen("del E:\test\*.txt",shell=False)
If you want to delete file with more than one extension then define those extensions in tuple like below
import os
def purge(dir):
files = os.listdir(dir)
ext = ('.txt', '.xml', '.json')
for file in files:
if file.endswith(ext):
print("File -> " + os.path.join(dir,file))
os.remove(os.path.join(dir,file))

Categories