Computer >> कंप्यूटर >  >> प्रोग्रामिंग >> Python

पायथन - मास्टर फाइल में कई फाइल डेटा लिखें

फ़ाइल प्रबंधन किसी भी वेब एप्लिकेशन का एक महत्वपूर्ण हिस्सा है।

फ़ाइल बनाने, पढ़ने, अपडेट करने और हटाने के लिए पायथन के कई कार्य हैं।

किसी मौजूदा फ़ाइल में लिखने के लिए, आपको ओपन () फ़ंक्शन में एक पैरामीटर जोड़ना होगा -

"ए" - संलग्न करें - फ़ाइल के अंत में संलग्न होगा

"w" - लिखें - किसी भी मौजूदा सामग्री को अधिलेखित कर देगा

उदाहरण

import os
# list the files in directory
lis = os.listdir('D:\\python' '\\data_files\\data_files')
print(lis)
tgt = os.listdir('D:\\python' '\\data_files\\target_file')  
file_dir ='D:\\python\\data_files\\data_files'
out_file = r'D:\\python\\data_files\\target_file\\master.txt'
ct = 0  
print('target file :', tgt)
try:
   # check for if file exists
   # if yes delete the file
   # otherwise data will be appended to existing file
   if len(tgt)>0:
      os.remove('D:\\python' '\\data_files\\target_file\\master.txt')
      open(tgt, 'a').close()
   else:
      # create an empty file
      open(tgt, 'a').close()
except:
   head = open('D:\\python' '\\data_files\\target_file\\master.txt', 'a+')
   line ='empno, ename, sal'
   # write header to output
   print(head, line)
   head.close()
   # below loop to write data to output file
   for line1 in lis:
      f_dir = file_dir+'\\'+line1
      # open files in read mode
      in_file = open(f_dir, 'r+')
      # open output in append mode
      w = open(out_file, 'a+')
      d = in_file.readline()
      d = in_file.readlines()
      w.write("\n")
      for line2 in d:
         print(line2)
         w.write(line2)    
      ct = ct + 1  
   w.close()      
#using pandas
import pandas as pd
# pd.read_csv creates dataframes
df1 = pd.read_csv('D:\python\data_files\data_files\emp_1.txt')
df2 = pd.read_csv('D:\python\data_files\data_files\emp_2.txt')
df3 = pd.read_csv('D:\python\data_files\data_files\emp_3.txt')
frames = [df1, df2, df3]
# concat function concatenates the frames
result = pd.concat(frames)
# to_csv function writes output to file
result.to_csv('D:\\python\\data_files' '\\target_file\\master.txt', encoding ='utf-8', index = False)

  1. पायथन - एक CSV फ़ाइल में पांडा डेटाफ़्रेम कैसे लिखें?

    Python में CSV फ़ाइल में पांडा डेटाफ़्रेम लिखने के लिए, to_csv() . का उपयोग करें तरीका। सबसे पहले, हम सूचियों का एक शब्दकोश बनाते हैं - # dictionary of lists d = {'Car': ['BMW', 'Lexus', 'Audi', 'Mercedes', 'Jaguar', 'Bentley'],'Date_of_pur

  1. बेस 64 डेटा एन्कोडिंग पायथन का उपयोग कर रहा है

    बेस 64 मॉड्यूल में फ़ंक्शन बाइनरी डेटा को प्लेनटेक्स्ट प्रोटोकॉल का उपयोग करके ट्रांसमिशन के लिए उपयुक्त ASCII के सबसेट में अनुवाद करते हैं। एन्कोडिंग और डिकोडिंग फ़ंक्शन RFC 3548 में विनिर्देशों को लागू करते हैं, जो बेस16, बेस32, और बेस64 एल्गोरिदम को परिभाषित करता है, और वास्तविक मानक Ascii85 और

  1. पायथन का उपयोग करके कई फाइलों का नाम बदलें

    नाम बदलें () विधि का उपयोग Python3 में किसी फ़ाइल या निर्देशिका का नाम बदलने के लिए किया जाता है। नाम बदलें () विधि ओएस मॉड्यूल का एक हिस्सा है। os.rename के लिए सिंटैक्स () os.rename(src, dst) पहला तर्क src है जो नाम बदलने के लिए फ़ाइल का स्रोत पता है और दूसरा तर्क dstजो नए नाम के साथ गंतव्य है।