My path is /cygdrive/c/Users/MyAccount/MyBox/myDev/trunk
My python scripts are located in /cygdrive/c/Users/MyAccount/MyBox/myScript/delivery
I want to get list of all files in python script folder as follows:
script_dir = os.path.dirname(os.path.abspath(__file__))
print('script_dir %s' % script_dir)
for root, dirs, files in os.walk(script_dir):
for file_name in files:
print(file_name)
I can't get it work because my script_dir prints :
script_dir /cygdrive/c/Users/MyAccount/MyBox/myDev/trunk/myScript/delivery
How can I make it works?
Thanks
Related
*No one come in here and say use os.path.abspath() I tried, it literally doesn't do what I want it just gives everything the drive of the current directory which isn't what I want I want to have the real drive.
I am getting the full path without the drive
example:
\\FileHistory\\fun64\\RAMPAGE\\Data\\$OF\\6413
Its not specifying the drive.
Code:
import os
root_dir = '\\'
folders = []
for path, dirs, files in os.walk(root_dir):
for dir in dirs:
full_path = os.path.join(path, dir)
folders.append(full_path)
print(folders)
I want to Specify what drive the folder is on at the start
import os
import win32api
folders = []
for drive in win32api.GetLogicalDriveStrings().split('\000')[:-1]:
for path, dirs, files in os.walk(drive):
for dir in dirs:
full_path = os.path.join(path, dir)
folders.append(full_path)
print(folders)
Knowing the name of my folder "folder_name",
how can i easily get in python (script not in the same folder) the absolute path "/home/user/../path/to/../folder_name" ?
import os
directory = os.path.dirname(__file__)
file_path = os.path.join(directory, './filename.extension')
print(file_path)
I'm attempting to rename multiple files in a github repo directory on windows 10 pro
The file extensions are ".pgsql" (old) and ".sql" (rename to)
I'm using vscode (latest) and python 3.7 (latest)
I can do it, one folder at a time, but whenever I have tried any recursive directory code I've looked up on here I cant get it to work.
Currently working single directory only
#!/usr/bin/env python3
import os
import sys
folder = 'C:/Users/YOURPATHHERE'
for filename in os.listdir(folder):
infilename = os.path.join(folder,filename)
if not os.path.isfile(infilename): continue
oldbase = os.path.splitext(filename)
newname = infilename.replace('.pgsql', '.sql')
output = os.rename(infilename, newname)
I'd like to have it recursively start in a directory and change only the file extensions specified to .sql in all sub directories as well on windows, for example
folder = 'C:/Users/username/github/POSTGRESQL-QUERY/'
You can use os.walk(),
import os
folder = 'C:/Users/YOURPATHHERE'
for root, dirs, files in os.walk(folder):
for filename in files:
infilename = os.path.join(root,filename)
newname = infilename.replace('.pgsql', '.sql')
output = os.rename(infilename, newname)
So I have a group of folders with many subfolders in them, and each of those have subfolders have multiple .pyc files and .pyc.py inside them. I have made a script on deleting all of a certain extension. Now I'm trying to rename the extensions of the .pyc.py with this code:
import os
extensions = ('.py',)
print 'Changing to root directory...'
os.chdir('../')
print 'Current Directory:'
print os.getcwd()
os.chdir('TTOLD')
print "Current Directory:"
print os.getcwd()
def rename(filepath):
print "Renaming '%s'..." % filepath
os.rename(filepath, extension)
for root, folders, files in os.walk('.'):
for filename in files:
filepath = os.path.join(root, filename)
extension = os.path.splitext(filename)[1]
if os.path.splitext(filename)[1] in extensions:
rename(filepath)
print 'Finished'
This code isn't finished, and I don't exactly know how to finish it. Could anyone help?
I'm trying to make a script that copies all the files other than the zipped files from a source folder to another destination folder and extract zipped files from the source folder to the destination, this is what i have come till this far:
import os
import zipfile
import shutil
myPath = "Source dir"
for root, dirs, files in os.walk(myPath):
for file in files:
if file.endswith('.zip'):
fh = open(file, 'rb')
z = zipfile.ZipFile(fh)
for name in z.namelist():
outpath = "destination dir"#Put the name of the destination folder
z.extract(name, outpath)
fh.close()
else:
fileList = os.listdir('source dir')
for f in fileList:
shutil.copy2(f, 'destination directory')
The code shows no error but there is no output too.
From Python Standard Library To get a full path (which begins with top) to a file or directory in dirpath, do os.path.join(dirpath, name) so you should write
fh = open(so.path.join(root,file))
to have the correct path.