Question

Python code to randomly select a file in a directory and then the next 9 consecutive...

Python code to randomly select a file in a directory and then the next 9 consecutive files in the same directory
0 0
Add a comment Improve this question Transcribed image text
Answer #1
Python Program

#!/usr/bin/env python
# coding: utf-8

# ## Files creation

# In[55]:


# Creates 30 sample files from 0 - 30 in sample folder

for i in range(30):
    file = open("./sample/sample"+str(i)+".txt", "w") 
    file.write("Your text goes here") 
    file.close() 


# ## Select random File

# In[56]:


# import random & os package

import os, random

# select random file from sample folder

random_file = random.choice(os.listdir("./sample/"))

print("random file = ",random_file)

#list files in sample folder and stores in files

files = os.listdir('./sample/')

#prints files

print ("\nFiles        ---------------------------- \n ")
print (files)


# ## Select 9 consecutive files 

# In[57]:

# finds the random file position from files and prints next 9 files

for i in range(len(files)):
    if files[i] == random_file:
        file_position = i
        
## prints next 9 files

files[file_position+1:file_position+10]


# In[ ]:

Output

Add a comment
Know the answer?
Add Answer to:
Python code to randomly select a file in a directory and then the next 9 consecutive...
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: 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)

  • Select your answer from here. absolute pathname to the file called xyz changes the directory to...

    Select your answer from here. absolute pathname to the file called xyz changes the directory to the parent of the current directory. lists current directory files including the invisible files sends xyz file to the line printer deletes the directory called xyz displays the content of the file called xyz displays the current directory pathname cancels the printing job on the 1p 1 printer confirms the deletion of the xyz file before deleting it lists the current directory in long...

  • Can you write a Python 2.7 function that takes a directory path, and prints the size,...

    Can you write a Python 2.7 function that takes a directory path, and prints the size, in bytes, of all the files, ignoring sub-directories, reverse sorted by size, with the total in the following exact format: file_name_2: 200000 file_name_1: 100000 file_name_3: 1000 > 3 File(s) - Total: 301000. Provide the copyable code.

  • IT PYTHON QUESTION1 Consider the following Python code, where infile.txt and outfile.txt both exist in the current directory 'z' ) 。1d = open ( ' infile. txt ' , for line in o...

    IT PYTHON QUESTION1 Consider the following Python code, where infile.txt and outfile.txt both exist in the current directory 'z' ) 。1d = open ( ' infile. txt ' , for line in old: new.write (line) new.write') ne«.close () old.close) Which of the following options best describes the purpose or outcome of this code? O A copy of the file infile.txt is made (except in double line spacing) and saved as outfile.txt in the current directory. O A copy of the...

  • in python in the simplest way I have a file “a.txt” under the directory /temp/, the...

    in python in the simplest way I have a file “a.txt” under the directory /temp/, the file contains some company names, the format is like that: IBM APPLE cisco Microsoft …… Each line contains only one company name. Among these company names, some are capitalized such as IBM, APPLE, some are not, such as cisco. Write a program to read this file a.txt and convert all names to uppercase and save them to the file name “b.txt” in the same...

  • In Python Provide me with your code file, output file and the text file Create a...

    In Python Provide me with your code file, output file and the text file Create a file having different integers than the first one. Save it as numbers1. txt . Write a program that reads all the number and calculates their average. Important: The two files that you are creating will contain different numbers.

  • 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);

  • #PYTHON# In this exercise you will write code which loads a collection of images (which are...

    #PYTHON# In this exercise you will write code which loads a collection of images (which are all the same size), computes a pixelwise average of the images, and displays the resulting average. The images below give some examples that were generated by averaging "100 unique commemorative photographs culled from the internet" by Jason Salavon. Your program will do something similar. Write a function in the cell below that loads in one of the sets of images and computes their average....

  • 1. Write a function that takes as input a directory. The function will search through that...

    1. Write a function that takes as input a directory. The function will search through that directory and its full subdirectory tree, building a count of the files by their extensions. As in the lecture, use a dictionary to keep track of the counts. The return-value of this function will be a dictionary of (key,value) pairs where the key will be the file extension and the value will be the number of files with that extension. For example, for the...

  • Python Problem Write a .py file like nameFiles.py. In that .py, create a function that strips the...

    Python Problem Write a .py file like nameFiles.py. In that .py, create a function that strips the first letters from all file names in a directory. If the new name is a duplicate of an existing name, Python will throw an exception. Handle that exception by adding “dup” a number as part of the files new name. Call that function from a Jupyter Notebook, passing in the number of characters to delete and the file path.

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
Active Questions
ADVERTISEMENT