Question

Python Write the list of all the files in the directory ptest_0 , in a text...

Python

Write the list of all the files in the directory ptest_0 , in a text file contents.txt (this file already exists there.) in separate lines.

1)

import glob
import shutil
import os


Beginning_folder = r"/home/MikeyV/Laptop/test_0/learn0_0";

Last_folder = "/home/MikeyV/Laptop/ptest_0";
try:
   os.makedirs(last_folder);

except;
   print"Folder already exists...";
for txt _file in glob.glob(beginning_folder+"\\*txt"):
   Shuril.copy2(txt_file,last_folder);

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The requirement according to the problem is for a given path of directory we have to get the list of all files in that directory and save the list of all files in the directory into a contents.txt file.

from os import listdir
from os.path import isfile, join

# Below I define the path to directory in which I have to get the list

dir_path="E:\\python\\test"
# above I have taken a sample path of my own directory you
# replace it with the you own required path of directory

try:
#Getting list of all file in given location
onlyfiles = [f for f in listdir(dir_path) if isfile(join(dir_path, f))]
# opening the contents.txt file and write each file in list in one
# single line in the file
with open('contents.txt','w') as fp:
for i in onlyfiles:
fp.write(i+'\n')
except FileNotFoundError as e:
print(e)

The above picture shows the different files in the test directory which I used in the program code

The contents.txt file which contains output image is shown below

I have not used some library which is said in sample code you have given because we can write code to the question given without using them easily without any problem and it still easy and fast than that.

I hope you got the answer and understand it.

Thank You:):)


Add a comment
Know the answer?
Add Answer to:
Python Write the list of all the files in the directory ptest_0 , in a text...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python, I need help with glob. I have a lot of data text files and want...

    Python, I need help with glob. I have a lot of data text files and want to order them. However glob makes it in the wrong order. Have: data00120.txt data00022.txt data00045.txt etc Want: data00000.txt data00001.txt data00002.txt etc Code piece: def last_9chars(x):     return(x[-9:]) files = sorted(glob.glob('data*.txt'),key = last_9chars) whole code: import numpy as np import matplotlib.pyplot as plt import glob import sys import re from prettytable import PrettyTable def last_9chars(x):     return(x[-9:]) files = sorted(glob.glob('data*.txt'),key = last_9chars) x = PrettyTable() x.field_names =...

  • JAVA: create a program that reads all text files in a directory, calculates the occurrences of...

    JAVA: create a program that reads all text files in a directory, calculates the occurrences of words, and saves the result in one single output text file. assume that you are in a directory called Desktop/programdirectory, where you have multiple text files. textfile1.txt : "how are you" textfile2.txt: "great thank you" textfile3.txt: "great to see you. see you later" Then, your output.txt should have: how: 1 are: 1 you: 4 great: 2 thank: 1 to: 1 see: 2 later: 1

  • Write a program to count a number of files for each extension in the given directory....

    Write a program to count a number of files for each extension in the given directory. The program should take a directory name as argument and print count and extension for each available file extension. For example, the output would be something like this: python counter.py \yourpathhere 14 py 4 txt 1 csv

  • 1.Write a find command that will display the inode number of all files in the working...

    1.Write a find command that will display the inode number of all files in the working directory (only) that contain the string "#include". 2.Write an ls command with a glob specification that will list the names of all files in the working directory that are exactly 10 characters long, begin with w, x, y, or z, and do not contain a "." character (i.e. no file extension). 3.Write an ls command with a glob specification that will list the names...

  • Write a python program that prompts the user for the names of two text files and...

    Write a python program that prompts the user for the names of two text files and compare the contents of the two files to see if they are the same. If they are, the scripts should simply output “Yes”. If they are not, the program should output “No”, followed by the first lines of each file that differ from each other. The input loop should read and compare lines from each file. The loop should break as soon as a...

  • Write a Java program that lists all the files in a directory and their subdirectories, that...

    Write a Java program that lists all the files in a directory and their subdirectories, that mimics the Unix ls command or the Windows dir command. Note that when a directory is encountered we do not immediately print its contents recursively. Rather, as we scan each directory, place any subdirectory in a List. After the directory entries are printed then process each subdirectory recursively. For each file that is listed, include the modification time, the file size, and if it...

  • Python: Please write code to rename all files in the working directory using its last modification...

    Python: Please write code to rename all files in the working directory using its last modification date. Hint: to rename a file, please use os.rename(old_name, new_name)

  • Reading and Writing Files in a Directory Create a program that provides a listing of all...

    Reading and Writing Files in a Directory Create a program that provides a listing of all the files in a directory. The program should be a Java application that accepts a single argument into the args array of the main() method. This argument identifies the name of a directory. The application should make sure that this filename really does identify a directory and then list all of the files in the directory. For each file, list whether the file is...

  • Python Assignment Tasks 1. Place the two provided plaintext files (file_1.txt, file_2.txt) on your desktop. 2....

    Python Assignment Tasks 1. Place the two provided plaintext files (file_1.txt, file_2.txt) on your desktop. 2. Write a Python program named fun_with_files.py. Save this file to your desktop as well. 3. Have your program write the Current Working Directory to the screen.   4. Have your program open file_1.txt and file_2.txt, read their contents and write their contents into a third file that you will name final.txt . Note: Ponder the open‐read/write‐close file operation sequence carefully. 5. Ensure your final.txt contains...

  • use python IDEL Please highlight the answer Problem 3 Files. Read the following code and answer...

    use python IDEL Please highlight the answer Problem 3 Files. Read the following code and answer the next five questions about it. The contents of the files list 1.txt and list2.txt are the following (given side by side): list 1. txt content: list2.txt content 2 apples 2 apples 3 oranges 1 loaf of bread 2 eggs 2 eggs 1 cereal box # the program code starts here filename1 = "list 1.txt" filename2 - "list2.txt" filename3 - "listdiff.txt" with open (filename1,"r")...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT