Get absolute paths of all files in a directory
os.path.abspath
makes sure a path is absolute. Use the following helper function:
import os
def absoluteFilePaths(directory):
for dirpath,_,filenames in os.walk(directory):
for f in filenames:
yield os.path.abspath(os.path.join(dirpath, f))
Getting the absolute paths of all files in a folder, without traversing the subfolders
It's easy to adapt that solution: Call os.walk()
just once, and don't let it continue:
root, dirs, files = next(os.walk(my_dir, topdown=True))
files = [ os.path.join(root, f) for f in files ]
print(files)
Getting a list of all files (with absolute path) recursively and their uid with Python
import os
import glob
for filename in glob.iglob('./**/*', recursive=True):
print(os.path.abspath(filename), os.stat(filename).st_uid)
Needs Python 3.5 or higher
Use a Glob() to find files recursively in Python?
Get absolute path of files in sub-directory
Use os.path.join:
root = '/tmp/project'
files = [os.path.join(root, d, f) for d in os.listdir(root) for f in os.listdir(os.path.join(root, d))]
print files
Output:
['/tmp/project/auth/__init__.py', '/tmp/project/controllers/__init__.py']
How do you get full paths of all files in a directory in Go?
If you want to see a full path, you should start with a full path. .
is a relative path.
You can get the working path with os.Getwd
path, err := os.Getwd()
// handle err
printFiles(path)
The rest is simply appending the file name to the directory path. You should use the path/filepath
package for that:
for _, file := range fileInfos {
fmt.Println(filepath.Join(path, file.Name())
}
python os: get all absolute file paths under a certain directory
filenames
is just a list of the names of the files, and doesn't store any directory information. That comes from dirpath
, which you're ignoring at the moment.
import os
for dirpath, dirnames, filenames in os.walk('/Users/Me/Desktop'):
for file in filenames:
print os.path.join(os.path.relpath(dirpath, '/Users/Me/Desktop'), file)
Edit: added os.path.relpath
to give relative rather than absolute paths. See this answer.
How to get absolute paths of files in a directory?
Using HDFS API :
package org.myorg.hdfsdemo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class HdfsDemo {
public static void main(String[] args) throws IOException {
Configuration conf = new Configuration();
conf.addResource(new Path("/Users/miqbal1/hadoop-eco/hadoop-1.1.2/conf/core-site.xml"));
conf.addResource(new Path("/Users/miqbal1/hadoop-eco/hadoop-1.1.2/conf/hdfs-site.xml"));
FileSystem fs = FileSystem.get(conf);
System.out.println("Enter the directory name :");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Path path = new Path(br.readLine());
displayDirectoryContents(fs, path);
}
private static void displayDirectoryContents(FileSystem fs, Path rootDir) {
// TODO Auto-generated method stub
try {
FileStatus[] status = fs.listStatus(rootDir);
for (FileStatus file : status) {
if (file.isDir()) {
System.out.println("This is a directory:" + file.getPath());
displayDirectoryContents(fs, file.getPath());
} else {
System.out.println("This is a file:" + file.getPath());
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Related Topics
How to Store Multiple Strings as One Variable
Flask API Typeerror: Object of Type 'Response' Is Not Json Serializable
I Need to Code a 1 22 333 4444 Pattern in Python With While Loops
How to Save Training History on Every Epoch in Keras
How to Delete a Record by Id in Flask-Sqlalchemy
Remove the Selected Elements from the Image in Opencv
Printing Each Letter of a Word + Another Letter - Python
How to Transform Floats to Integers in a List
How to Insert Text At Line and Column Position in a File
How to Locate the Input Within Div
Get First Date and Last Date of Current Quarter in Python
How Do Convert a Pandas Dataframe to Xml
How to Display a Float With Two Decimal Places
Multiprocessing: How to Use Pool.Map on a Function Defined in a Class
Best Practice to Run Multiple Spark Instance At a Time in Same Jvm